i) 必须写给赋予儿童身份的标签
我的xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><tree>
<declarations>
<attributeDecl name="name" type="String"/>
</declarations>
<branch>
<branch>
<branch>
<branch>
<branch>
<attribute name="name" value=""/></branch>
<branch>
<attribute name="name" value=""/></branch>
<attribute name="name" value="Do you have this condition for more than a weeks time"/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have watery stool"/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Excess Stool"/></branch>
<branch>
<branch>
<branch>
<branch>
<attribute name="name" value="Do you have chills"/>
<branch>
<attribute name="name1" value=""/>
</branch>
<branch>
<attribute name="name2" value=""/>
</branch>
</branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have high fever for more than a weeks time "/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Was your peak temperature greater than 104"/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="High Fever"/></branch>
<branch>
<branch>
<branch>
<branch>
<branch>
<attribute name="name" value=""/>
</branch>
<branch>
<attribute name="name" value=""/></branch>
<attribute name="name" value="Duration of the headache spans for more than a day "/>
</branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have headache more than 5 times a day "/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have pain in the left part of the head "/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Bodyache"/></branch>
<attribute name="name" value="Malaria"/></branch>
</tree>
我的法典:
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XmlQuestionwrite {
Document dom = null;
Node node = null;
String filepath = "data/chi-ontology.xml";
String itemclickedonapplet = null;
String consent = "Yes";
public void questionWrite() {
// String attributechange = Question;
// String consentvalue = consent;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse(filepath);
Element docele = dom.getDocumentElement();
// Node branch = dom.createElement("branch");
// Element attribute = dom.createElement("attribute");
NodeList nl = docele.getElementsByTagName("attribute");
if (nl != null && nl.getLength() > 0) {
for (int i = 0; i < nl.getLength(); i++) {
Element sl = (Element) nl.item(i);
String val = sl.getAttribute("value").trim();
if (val.equals("Do you have chills")) {
// Node parentnode = sl.getParentNode();
NodeList ashu = sl.getParentNode().getChildNodes();
for (int i1 = 0; i1 < ashu.getLength(); i1++) {
Node node = ashu.item(i1);
if (node.getNodeType() != Node.ELEMENT_NODE)
continue;
Element ele = (Element) ashu.item(i1);
if (ele.getNodeName().equals("branch")){
NodeList questionappend=ele.getElementsByTagName("attribute");
for (int count = 0; i < questionappend.getLength(); count++){
Element questionitem = (Element) questionappend.item(i);
String appendvalue = sl.getAttribute("name").trim();
if (appendvalue.equals("name1") && consent.equals("Yes") ) {
questionitem.setAttribute("value", "this is the question for the yes item ");
}
else if(appendvalue.equals("name2") && consent.equals("No")){
questionitem.setAttribute("value", "this is the question for the no item ");
}
}
}
}
}
}
}// if ends here
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(dom);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (SAXException se) {
se.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
public static void main (String args []) {
XmlQuestionwrite qwrite= new XmlQuestionwrite();
qwrite.questionWrite();
}
}
请就此帮助我。
感谢dma K ,it is fun to Explor xpath , 而实际上,为了方便起见,我称之为属地名称1和名称2
真正意义上的xml 只是不同的因素,即“你有钱”的兄弟姐妹名称。
因此,真实的xml将视同法典一样,把属性定在左侧。
测试。
<?xml version="1.0" encoding="UTF-8" standalone="no"?><tree>
<declarations>
<attributeDecl name="name" type="String"/>
</declarations>
<branch>
<branch>
<branch>
<branch>
<branch>
<attribute name="name" value=""/></branch>
<branch>
<attribute name="name" value=""/></branch>
<attribute name="name" value="Do you have this condition for more than a weeks time"/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have watery stool"/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Excess Stool"/></branch>
<branch>
<branch>
<branch>
**<branch>
<attribute name="name" value="Do you have chills"/>
<branch>
<attribute name="name" value=""/>
</branch>
<branch>
<attribute name="name" value=""/>
</branch>**
</branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have high fever for more than a weeks time "/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Was your peak temperature greater than 104"/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="High Fever"/></branch>
<branch>
<branch>
<branch>
<branch>
<branch>
<attribute name="name" value=""/>
</branch>
<branch>
<attribute name="name" value=""/></branch>
<attribute name="name" value="Duration of the headache spans for more than a day "/>
</branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have headache more than 5 times a day "/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Do you have pain in the left part of the head "/></branch>
<branch>
<attribute name="name" value="No"/></branch>
<attribute name="name" value="Bodyache"/></branch>
<attribute name="name" value="Malaria"/></branch>
</tree>
我用黑体 bold了这一部分,需要加以改动。