user1746460
user1746460

Reputation:

How to get all nodes from a DOM Document (Elements, Attributes etc)

I am trying to get all nodes that are part of a document and am only able to get the element nodes, using getElementByTagName("*"). But this does not return the other nodes viz. attribute and other nodes.

Is there an api available or do I have to iterate further on these elements to get there attribute nodes?

This is what I am trying now; Need to know if there is any other way or a direct api for the same

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;
    }

Thanks in advance

Upvotes: 0

Views: 8661

Answers (3)

Gianni B.
Gianni B.

Reputation: 2731

You have to use getChildNodes() that give you all the nodes (attributes, elements 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);
        }
    }
}

Upvotes: 0

Ataur Rahman Munna
Ataur Rahman Munna

Reputation: 3917

This is worked for me.

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())
            }

EDIT :

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);
                }

Upvotes: 1

Borislav Stoilov
Borislav Stoilov

Reputation: 3697

if you are using selenium ( just guessing ), you should use findElements(By.xpath("")), the tag name is not treated as regex, that's why the '' is not working.

Upvotes: 0

Related Questions