2

我想在 Java 中获取 XML 元素的兄弟姐妹。

xml文件如下:

  <parent>
    <child1> value 1 </child1>
    <child2> value 2 </child2>
    <child3> value 3 </child3>
  </parent>

我在 JAVA 中使用 DOM 解析器的代码如下:

 package dom_stack;

 import java.io.File;
 import java.io.IOException;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 import org.w3c.dom.Document;
 import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;


 public class DOM_stack {


     public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {


      File file = new File("root.xml");
      if (file.exists()){
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

            Document doc;
             doc = dbf.newDocumentBuilder().parse("root.xml");


            NodeList elemNodeList = doc.getElementsByTagName("child1");                 

            for(int j=0; j<elemNodeList.getLength(); j++) {

                System.out.println("The node's value that you are looking now is : " +  elemNodeList.item(j).getTextContent());
                System.out.println(" Get the Name of the Next Sibling " + elemNodeList.item(j).getNextSibling().getNodeName());
                System.out.println(" Get the Value of the Next Sibling " + elemNodeList.item(j).getNextSibling().getNodeValue());

            }


        }//if file exists
     }
    }

不幸的是,结果是:

    run:
    The node's value that you are looking now is :  value 1 
     Get the Name of the Next Sibling #text
     Get the Value of the Next Sibling 

它应该是:

    run:
    The node's value that you are looking now is :  value 1 
     Get the Name of the Next Sibling child2
     Get the Value of the Next Sibling value2

那么,我怎样才能得到理想的输出呢?

提前致谢

4

4 回答 4

4

或者,您可以使用 XPath 轻松完成:

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

    // Select the first child of the root element
    Element c1 = (Element) xp.evaluate("/parent/*[1]", doc,
            XPathConstants.NODE);

    // Select the siblings of the first child
    NodeList siblings = (NodeList) xp.evaluate("following-sibling::*", c1,
            XPathConstants.NODESET);
    for (int i = 0; i < siblings.getLength(); ++i) {
        System.out.println(siblings.item(i));
    }
于 2013-07-14T21:08:35.747 回答
4
<parent>
  <child1> value 1 </child1>
  <child2> value 2 </child2>
  <child3> value 3 </child3>
</parent>

你得到的是和元素之间的空白文本节点。child1child2

您需要继续遍历兄弟姐妹以跳过空格、注释等,以获取下一个元素节点

Node child1 = elemNodeList.item(j);
Node sibling = child1.getNextSibling();
while (!(sibling instanceof Element) && sibling != null) {
  sibling = sibling.getNextSibling();
}
System.out
      .println(" Get the Name of the Next Sibling " + sibling.getNodeName());
System.out
      .println(" Get the Value of the Next Sibling " + sibling.geTextContent());
于 2013-07-14T19:07:51.937 回答
2

当您调用此行时:

NodeList elemNodeList = doc.getElementsByTagName("child1");

您正在收集名称为“child1”的所有元素的列表。您期望名称为“child2”和“child3”的元素也会出现在其中。

你可以做的是Element通过doc.getDocumentElement(). 然后,您可以通过调用然后遍历该列表来获取NodeList该文档的子节点。rootElement.getChildNodes()

于 2013-07-14T17:14:58.907 回答
1

由于@McDowell 的解决方案对我不起作用,我对其进行了一些更改并使其正常工作,因此Node sibling将成为“下一个”xml 标记(例如,如果Node current<child1>Node sibling则将是<child2>):

Node sibling = current.getNextSibling();
while (null != sibling && sibling.getNodeType() != Node.ELEMENT_NODE) {
    sibling = sibling.getNextSibling();
}
于 2015-07-01T15:35:42.793 回答