Java XML Node Find findNodesByTagName(Document document, String tagName)

Here you can find the source of findNodesByTagName(Document document, String tagName)

Description

find Nodes By Tag Name

License

Apache License

Declaration

public static Node[] findNodesByTagName(Document document, String tagName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

import org.w3c.dom.Document;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static Node[] findNodesByTagName(Document document, String tagName) {
        ArrayList<Node> nodes = new ArrayList<Node>();

        NodeList foundNodes = document.getElementsByTagName(tagName);
        for (int i = 0; i < foundNodes.getLength(); i++)
            nodes.add(foundNodes.item(i));

        Node[] returnNodes = new Node[nodes.size()];
        return nodes.toArray(returnNodes);
    }//  w  w  w .j ava2 s  .  c o  m

    public static Node[] findNodesByTagName(Node parentNode, String tagName) {
        ArrayList<Node> nodes = new ArrayList<Node>();

        for (int i = 0; i < parentNode.getChildNodes().getLength(); i++) {
            Node node = parentNode.getChildNodes().item(i);

            if (node.getNodeName().equals(tagName))
                nodes.add(node);

            if (node.hasChildNodes()) {
                Node[] foundChildNodes = findNodesByTagName(node, tagName);
                for (int j = 0; j < foundChildNodes.length; j++)
                    nodes.add(foundChildNodes[j]);

            }
        }

        Node[] returnNodes = new Node[nodes.size()];
        return nodes.toArray(returnNodes);
    }
}

Related

  1. findNodeByTagName(Document document, String tagName)
  2. findNodeByXpath(org.w3c.dom.Document doc, String xpathEx)
  3. findNodeIndex(Node node)
  4. findNodeIndex(Node node)
  5. findNodeLong(Node node)
  6. findNodesNamed(Node node, String lookForName, Collection ret)
  7. findNodeValue(Node aNode, String aName)
  8. findNodeWithAttrValue(Document doc, String elementName, String attrName, String attrValue)