开发者

html parsing with help of DOM

开发者 https://www.devze.com 2023-03-27 20:46 出处:网络
i have to write to the tagwhich has attribute child as my xml : <?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><tree>

i have to write to the tag which has attribute child as

my 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>

my code :

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 "); 
                            }
                  开发者_StackOverflow中文版           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();
    }
}

Please help me on that .

Thanks dma_K ,it was fun to explore xpath , actually for my convenience i named the name attribute of attribute as name1 and name2

in the real real xml only differentiating factor is the sibling node name "Do you have chills"

so the real xml will look somrthing like with code to set attribute in the attribute tag of

test.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>

I have bolded that part off xml which needs to be altered .


The approach you suggest is a kind of suicide... More easier and elegant is to use XPath:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(getClass().getResourceAsStream("test.xml"));

XPath xpath = XPathFactory.newInstance().newXPath();

NodeList nodeList = (NodeList) xpath.evaluate("//branch//attribute", document, XPathConstants.NODESET);

for (int i = 0, len = nodeList.getLength(); i < len; i++) {
    final Element attributeNode = (Element) nodeList.item(i);

    String value = attributeNode.getAttribute("name");

    if (value.equals("name1")) {
        attributeNode.setAttribute("value", "this is the question for the yes item ");
    }
    else if (value.equals("name2")) {
        attributeNode.setAttribute("value", "this is the question for the no item ");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号