2016-01-20 45 views
0

我想要获取属于文档一部分的所有节点,并且只能使用getElementByTagName("*")获取元素节点。但是这不会返回其他节点即,属性和其他节点。如何从DOM文档(元素,属性等)获取所有节点

是否有api可用或者我必须对这些元素进一步迭代以获得属性节点?

这就是我现在要做的;要知道,如果有任何其他方式或事先直接的API相同

private static List<Node> getAllNodes(Document doc) { 
     List<Node> returnList = new LinkedList<>(); 
     NodeList nodes = doc.getElementsByTagName("*"); 

     for (int index = 0; index < nodes.getLength(); index++) { 
      returnList.add(nodes.item(index)); 

      NamedNodeMap attribList = nodes.item(index).getAttributes(); 
      if (attribList == null) { 
       continue; 
      } 

      for (int j = 0; j < attribList.getLength(); j++) { 
       returnList.add(attribList.item(j)); 
      } 
     } 
     return returnList; 
    } 

感谢

+0

请粘贴您的DOM节点文件。 –

+0

请注意,属性的语义随[DOM4](属性不再是节点)而改变,它们不再实现Nodes接口。这可能与java无关,但是DOM树的节点与附加到节点的属性之间的语义区别仍然值得注意。 – the8472

回答

0

你必须使用getChildNodes(),让你的所有节点(属性,元素ECC ...)

Document doc; //Your Document class 
printChild(doc); 

public void printChild(Node node) 
{ 
    NodeList childNodes = node.getChildNodes(); 
    System.out.println("Node: " + node.getNodeType() + ", " + node.getLocalName()); 
    for(int i = 0; i < childNodes.getLength(); i++) 
    { 
     Node childNode = childNodes.item(i); 
     if(childNode.hasAttributes()) 
     { 
      System.out.println("Attributes: " + childNode.getAttributes()); //just an example... 
      //Here you can iterate over each attributes to do something 
     } 

     if(childNode.hasChildNodes()) 
     { 
      System.out.println(""); //just an empty string 
      printChild(childNode); 
     } 
    } 
} 
+0

提供所有节点,但仅限当前节点。在文档节点的情况下,我只获得文档节点的直接子节点,我需要该文档中的所有节点。 – Sapan

0

如果你正在使用硒(只是猜测),你应该使用findElements(By.xpath(“”)),标签名称不被视为正则表达式,这就是为什么‘’不工作。

+0

我想在java中使用这个,不使用硒 – Sapan

0

这对我很有用。

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
      Document doc = dBuilder.parse(file); 
      //your file object put here. 
      doc.getDocumentElement().normalize(); 
      NodeList nList = doc.getElementsByTagName("*"); 
      for (int i = 0; i < nList.getLength(); i++) { 
       Node node = nList.item(i); 

       Element element = (Element) node; 
       System.out.println(element.getNodeName()) 
      } 

编辑:

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
       DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
       Document doc = dBuilder.parse(file); 
       //your file object put here. 
       doc.getDocumentElement().normalize(); 
       NodeList nList = doc.getElementsByTagName("*"); 
       for (int i = 0; i < nList.getLength(); i++) { 
        Node node = nList.item(i); 

        Element element = (Element) node; 
        System.out.println(element.getNodeName()); 
        String name = element.getAttribute("name"); 
        System.out.println(name); 
       } 
+0

这将返回所有类型为Element的节点,我也想要这些元素的所有属性。 – Sapan

+0

请在这里粘贴你的文档。 –

+0

该要求适用于任何xml。我们应该能够得到“人”节点“性别”节点和“名字”节点 – Sapan

相关问题