Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaDOM中迭代所有XML节点生成_Java_Xml_Dom - Fatal编程技术网

在JavaDOM中迭代所有XML节点生成

在JavaDOM中迭代所有XML节点生成,java,xml,dom,Java,Xml,Dom,我想检查XML文档中是否包含“person”元素。我可以非常简单地检查所有第一代元素: NodeList nodeList = root.getChildNodes(); for(int i=0; i<nodeList.getLength(); i++){ Node childNode = nodeList.item(i); if (childNode.getNodeName() == "person") { //do something with it } } 正

我想检查XML文档中是否包含“person”元素。我可以非常简单地检查所有第一代元素:

NodeList nodeList = root.getChildNodes();
for(int i=0; i<nodeList.getLength(); i++){
  Node childNode = nodeList.item(i);
  if (childNode.getNodeName() == "person") {
     //do something with it
  }
}

正如mmyers所说,您可以使用递归来解决这个问题

doSomethingWithAll(root.getChildNodes());

void doSomethingWithAll(NodeList nodeList)
{
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        if (childNode.getNodeName().equals("person")) {
            //do something with it
        }

        NodeList children = childNode.getChildNodes();
        if (children != null)
        {
            doSomethingWithAll(children);
        }
    }
}
dosomethingwhithall(root.getChildNodes());
void dosomethingwhithall(节点列表节点列表)
{
for(int i=0;i
这就是XPath的用途。要获取名为“person”的所有元素,请使用以下表达式:

//person
直接使用JDK的XPathAPI可能会很痛苦。我更喜欢在实用XML库中编写的包装器:

这里是我写的一个教程(一般是关于JDK XPath的,但提到了XPathWrapper):

我看到了三种可能性(其中两种其他人已经回答了):

  • 使用递归
  • 使用XPath(可能有点过分了) 对于这个问题,但是如果你有 有很多这样的疑问 肯定是要探索的东西)。 利用格雷戈里的帮助;A. 快速查看api表明 使用起来有点痛苦 直接的
  • 如果您拥有的实际上是一个
    文档
    (即如果
    根目录
    是一个
    文档
    ),您可以使用
    Document.getElementsByTagName

  • 除了
    Document.getElementsByTagName()
    XPath
    之外,您还可以使用我创建的库来简化XML访问和操作。jOOX包装了标准JavaAPI,并添加了类似实用程序的方法。然后,您的Python代码片段将转换为以下Java代码:

    // Just looking for tag names
    for (Element person : $(tree).find("person")) {
      personlist.append(person);
    }
    
    // Use XPath for more elaborate queries
    for (Element person : $(tree).xpath("//person")) {
      personlist.append(person);
    }
    

    以下是格式化版本:

    Element root = xmlData.getDocumentElement();  
    NodeList children = root.getChildNodes(); 
    
    public void doSomethingWithAllToConsole(NodeList nodeList, String tabs)
    {
        for(int i=0; i<nodeList.getLength(); i++){
    
          //print current node & values
          Node childNode = nodeList.item(i);
          if(childNode.getNodeType()==Node.ELEMENT_NODE){
              System.out.print(tabs + childNode.getNodeName());
              if(childNode.getFirstChild()!=null 
                      && childNode.getFirstChild().getNodeType()==Node.TEXT_NODE
                      && !StringUtil.isNullOrEmpty(childNode.getFirstChild().getNodeValue()) ){
                  System.out.print(" = " + childNode.getFirstChild().getNodeValue());
              }
              System.out.println();
          }
    
          //recursively iterate through child nodes
          NodeList children = childNode.getChildNodes();
          if (children != null)
          {
              doSomethingWithAllToConsole(children, tabs+"\t");
          }
        }
    }
    
    Element root=xmlData.getDocumentElement();
    NodeList childrends=root.getChildNodes();
    公共void doSomethingWithAllToConsole(节点列表、节点列表、字符串选项卡)
    {
    对于(int i=0;我认为您需要。
    
    Element root = xmlData.getDocumentElement();  
    NodeList children = root.getChildNodes(); 
    
    public void doSomethingWithAllToConsole(NodeList nodeList, String tabs)
    {
        for(int i=0; i<nodeList.getLength(); i++){
    
          //print current node & values
          Node childNode = nodeList.item(i);
          if(childNode.getNodeType()==Node.ELEMENT_NODE){
              System.out.print(tabs + childNode.getNodeName());
              if(childNode.getFirstChild()!=null 
                      && childNode.getFirstChild().getNodeType()==Node.TEXT_NODE
                      && !StringUtil.isNullOrEmpty(childNode.getFirstChild().getNodeValue()) ){
                  System.out.print(" = " + childNode.getFirstChild().getNodeValue());
              }
              System.out.println();
          }
    
          //recursively iterate through child nodes
          NodeList children = childNode.getChildNodes();
          if (children != null)
          {
              doSomethingWithAllToConsole(children, tabs+"\t");
          }
        }
    }