Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.*;

public class Main {
    public static Node findNode(Document d, String name) {

        Node body = d.getDocumentElement();

        return actualFindNode(body, name);
    }

    public static Node findNode(Node node, String name) {
        String nodeName = node.getNodeName();
        nodeName = nodeName.substring((nodeName.indexOf(":") != -1 ? nodeName.indexOf(":") + 1 : 0));
        name = name.substring((name.indexOf(":") != -1 ? name.indexOf(":") + 1 : 0));
        if (nodeName.equals(name)) {
            return node;
        }
        if (node.hasChildNodes()) {
            NodeList list = node.getChildNodes();
            int size = list.getLength();

            for (int i = 0; i < size; i++) {
                Node found = findNode(list.item(i), name);
                if (found != null)
                    return found;
            }
        }
        return null;
    }

    private static Node actualFindNode(Node node, String name) {

        String nodeName = node.getNodeName();
        nodeName = nodeName.substring((nodeName.indexOf(":") != -1 ? nodeName.indexOf(":") + 1 : 0));

        if (nodeName.equals(name)) {
            return node;
        }
        if (node.hasChildNodes()) {
            NodeList list = node.getChildNodes();
            int size = list.getLength();

            for (int i = 0; i < size; i++) {
                Node found = actualFindNode(list.item(i), name);
                if (found != null)
                    return found;
            }
        }
        return null;
    }
}