简体   繁体   中英

Parsing xml DOM child nodes with java

I have following structure of xml

<entities>
        <entity>
            <type>FieldTerminology</type>
            <relevance>0.732316</relevance>
            <sentiment>
                <type>negative</type>
                <score>-0.351864</score>
            </sentiment>
            <count>2</count>
            <text>financial crisis</text>
        </entity>
        <entity>
            <type>Company</type>
            <relevance>0.496572</relevance>
            <sentiment>
                <type>neutral</type>
            </sentiment>
            <count>1</count>
            <text>Goldman Sachs</text>
            <disambiguated>
                <name>Goldman Sachs</name>
                <subType>CompanyShareholder</subType>
                <website>http://www.gs.com/</website>
                <dbpedia>http://dbpedia.org/resource/Goldman_Sachs</dbpedia>
                <freebase>http://rdf.freebase.com/ns/m.01xdn1</freebase>
                <yago>http://yago-knowledge.org/resource/Goldman_Sachs</yago>
                <crunchbase>http://www.crunchbase.com/company/goldman-sachs</crunchbase>
            </disambiguated>
        </entity>

I am parsing all, only I cant access to child sentiment with this how can i access also "sentiment" in each entity node?

 NodeList feeds = docs.getElementsByTagName("entities");
            for (int i = 0; i < feeds.getLength(); i++) {
                Node mainNode = feeds.item(i);
                if (mainNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element firstElement = (Element) mainNode;
                    System.out.println("First element " + firstElement.getTagName());
                    NodeList forumidNameList = firstElement.getElementsByTagName("entity");

                    for (int j = 0; j < forumidNameList.getLength(); ++j) {
                        Element value = (Element) forumidNameList.item(j);

                        NodeList conditionList = value.getElementsByTagName("relevance");
                        for (int k = 0; k < conditionList.getLength(); ++k) {
                            Element condition = (Element) conditionList.item(k);
                            String conditionText = condition.getFirstChild().getNodeValue();
                            System.out.println("relevance " + conditionText);
                        }
                        NodeList conditionList1 = value.getElementsByTagName("type");
                        for (int k = 0; k < conditionList1.getLength(); ++k) {
                            Element condition = (Element) conditionList1.item(k);
                            String conditionText = condition.getFirstChild().getNodeValue();
                            System.out.println("type " + conditionText);
                        }
                        NodeList conditionList2 = value.getElementsByTagName("count");
                        for (int k = 0; k < conditionList2.getLength(); ++k) {
                            Element condition = (Element) conditionList2.item(k);
                            String conditionText = condition.getFirstChild().getNodeValue();
                            System.out.println("count " + conditionText);
                        }
                        NodeList conditionList3 = value.getElementsByTagName("text");
                        for (int k = 0; k < conditionList3.getLength(); ++k) {
                            Element condition = (Element) conditionList3.item(k);
                            String conditionText = condition.getFirstChild().getNodeValue();
                            System.out.println("text " + conditionText);
                        }

I need parse list of entities and also subnodes.

I have attempted to get you question solved,I made the following changes 1. Added the missing node parsing for <sentiment> 2. Improvised your parsing logic for <type> node as it would appear twice because of same name in the dom structure.

Note: I would still recommend to use JAXB, XPath for efficient xml parsing. Hope that helps Here the code.

    NodeList feeds = doc.getElementsByTagName("entities");
    for (int i = 0; i < feeds.getLength(); i++) {
        Node mainNode = feeds.item(i);
        if (mainNode.getNodeType() == Node.ELEMENT_NODE) {
            Element firstElement = (Element) mainNode;
            System.out.println("First element "
                    + firstElement.getTagName());
            NodeList forumidNameList = firstElement
                    .getElementsByTagName("entity");

            for (int j = 0; j < forumidNameList.getLength(); ++j) {
                Element value = (Element) forumidNameList.item(j);

                NodeList conditionList = value
                        .getElementsByTagName("type");
                for (int k = 0; k < conditionList.getLength(); ++k) {
                    Element condition = (Element) conditionList.item(k);
                    if (condition.getParentNode().getNodeName()
                            .equals("entity")) {
                        String conditionText = condition
                                .getFirstChild().getNodeValue();
                        System.out.println("type " + conditionText);

                    }
                }
                NodeList conditionList1 = value
                        .getElementsByTagName("relevance");
                for (int k = 0; k < conditionList1.getLength(); ++k) {
                    Element condition = (Element) conditionList1
                            .item(k);
                    String conditionText = condition.getFirstChild()
                            .getNodeValue();
                    System.out.println("relevance " + conditionText);
                }
                NodeList conditionList2 = value
                        .getElementsByTagName("sentiment");
                for (int k = 0; k < conditionList2.getLength(); ++k) {
                    Element condition = (Element) conditionList2
                            .item(k);
                    for (int l = 0; l < condition.getChildNodes()
                            .getLength(); ++l) {
                        Element condition2 = (Element) condition
                                .getChildNodes().item(l);
                        String conditionText = condition2
                                .getFirstChild().getNodeValue();
                        System.out
                                .println("sentiment " + conditionText);
                    }
                }
                NodeList conditionList3 = value
                        .getElementsByTagName("count");
                for (int k = 0; k < conditionList3.getLength(); ++k) {
                    Element condition = (Element) conditionList3
                            .item(k);
                    String conditionText = condition.getFirstChild()
                            .getNodeValue();
                    System.out.println("count " + conditionText);
                }
                NodeList conditionList4 = value
                        .getElementsByTagName("text");
                for (int k = 0; k < conditionList4.getLength(); ++k) {
                    Element condition = (Element) conditionList4
                            .item(k);
                    String conditionText = condition.getFirstChild()
                            .getNodeValue();
                    System.out.println("text " + conditionText);
                }
            }
        }
    }
output
----------------
First element entities
type FieldTerminology
relevance 0.732316
sentiment negative
sentiment -0.351864
count 2
text financial crisis
type Company
relevance 0.496572
sentiment neutral
count 1
text Goldman Sachs

Have you considered using a different parser? I've found that DOM struggled to handle more complex XML structures. I'd recommend trying JDOM which I found was much better at handling access issues such as yours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM