Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

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