如何获取NodeList中每个节点第一个child的值?

How to get the value of each node's first child in NodeList?

我想获取每个元素第一个 child 的元素名称和值。但我得到的是空值。我是 XML 的新手,我很困惑,只是想尝试使用漂亮的字体。根大约有15 children,它们都叫"DEvent",我希望我的输出是:

活动:星期三徒步旅行

事件:BOW - 狗拉雪橇!

等等

    <DNREvents>
<DEvent>
<event eid="1">Wednesday Trail Walks</event>
<date>2/03/2016</date>
<time>1-2:30 PM</time>
<description>...</description>
<location>Brown's Creek State Trail</location>
<cost>Free</cost>
<infoPhone>651-231-6968</infoPhone>
<infoEmail>LindaRadimecky@state.mn.us</infoEmail>
</DEvent>
<DEvent>
<event eid="2">BOW - Dog Mushing!</event>
<date>2/04/2016 thru 2/07/2016</date>
<time>Unknown</time>
<description>
This off the grid adventure is for those who want to actively participate in every aspect of learning to run your own small team of sled dogs through miles and miles of beautiful trails in the remote wilderness of northern Minnesota. Limited to 4 participants
</description>
<location>Grand Marais/Hovland</location>
<cost>5</cost>
<infoPhone>218-370-0283</infoPhone>
<infoEmail>linda@points_unknown.com</infoEmail>
</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
<DEvent>...</DEvent>
</DNREvents>

我的java显示结果-

Element DNR = root;
        NodeList EventList=DNR.getElementsByTagName("DEvent");
        for (int i=0; i< EventList.getLength();i++)
        {
            Node thisNode = EventList.item(i);
            Node child = thisNode.getFirstChild();
            String x = child.getNodeValue();
            System.out.println(thisNode+ x);
        }

这是一个工作示例。您代码中的 firstChild 是一个 #Text 节点,您需要获取该节点的 nextSibling() (因此您的事件节点毕竟不是第一个节点):

    import org.w3c.dom.*;
    import java.io.*;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;

    File xmlFile = new File("/data/test/test.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(xmlFile);

    NodeList eventList = doc.getElementsByTagName("DEvent");
    for (int i=0; i< eventList.getLength(); i++) {
        Node thisNode = eventList.item(i);

        if(thisNode.hasChildNodes()){
            NodeList event = thisNode.getChildNodes().getFirstChild().getNextSibling();
            String eid = event.getAttributes().getNamedItem("eid");
            String value = event.getFirstChild().getNodeValue();
            System.out.println("EID: " + eid + " Value: " + value + "\n");
        }
    }

请注意,如果您不执行额外的空值检查来验证 xml 是否具有正确的子节点,则此行将很脆弱。而且,这并不强制 xml 中子节点的顺序是您期望的顺序:

NodeList child = thisNode.getChildNodes().getFirstChild().getNextSibling();

话虽如此,我认为更好的实现方式是使用 XPath,它允许您 select 您想要的节点而无需手动向下遍历树并且更紧凑 'fail safe':

    XPath xpath = XPathFactory.newInstance().newXPath();
    String expression = "/DNREvents/DEvent/event";
    InputSource inputSource = new InputSource(new BufferedReader(new FileReader("/data/test/test.xml")));
    NodeList eventList = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);

    for (int i=0; i< eventList.getLength(); i++) {
        Node eventNode = eventList.item(i);

        String eid = xpath.evaluate("@eid", eventNode);
        String value = xpath.evaluate("./text()", eventNode);
        System.out.println("EID: " + eid + ", Value: " + value + "\n");
    }