English 中文(简体)
html 在人力部的帮助下
原标题:html parsing with help of DOM

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了这一部分,需要加以改动。

最佳回答
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签