如何从DOM文档(元素,属性等)获取所有节点

时间:2016-01-20 09:05:38

标签: java dom

我正在尝试使用getElementByTagName("*")获取属于文档的所有节点,并且只能获取元素节点。但这不会返回其他节点即。属性和其他节点。

是否有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;
    }

提前致谢

3 个答案:

答案 0 :(得分: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);
        }
    }
}

答案 1 :(得分:0)

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

答案 2 :(得分: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);
                }