用java解析xml-DOM子节点

用java解析xml-DOM子节点,java,xml,dom,xml-parsing,Java,Xml,Dom,Xml Parsing,我有以下xml结构 <entities> <entity> <type>FieldTerminology</type> <relevance>0.732316</relevance> <sentiment> <type>negative</type>

我有以下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>

野外术语
0.732316
消极的
-0.351864
2.
金融危机
单位
0.496572
中立的
1.
高盛
高盛
公司股东
http://www.gs.com/
http://dbpedia.org/resource/Goldman_Sachs
http://rdf.freebase.com/ns/m.01xdn1
http://yago-knowledge.org/resource/Goldman_Sachs
http://www.crunchbase.com/company/goldman-sachs
我在解析一切,只是我无法接触到孩子的情感 有了这个,我怎样才能访问每个实体节点中的“情绪”

 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);
                        }
nodelistfeeds=docs.getElementsByTagName(“实体”);
对于(int i=0;i

我需要解析实体和子节点的列表。

您考虑过使用不同的解析器吗?我发现DOM很难处理更复杂的XML结构。我建议尝试JDOM,我发现JDOM在处理像您这样的访问问题方面更好。

我试图解决您的问题,我做了以下更改 1.添加了
2.为
节点临时编写了解析逻辑,因为它在dom结构中会出现两次,因为名称相同

注意:我仍然建议使用JAXB和XPath进行有效的xml解析。希望这能有所帮助 这是代码

    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
nodelistfeeds=doc.getElementsByTagName(“实体”);
对于(int i=0;i