Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Node;

public class Main {

    public static Node getSingleNode(Node node, String path) {
        List nodeList = getNodes(node, path);

        if (nodeList.size() > 0) {
            return (Node) nodeList.get(0);
        }
        return null;
    }

    public static List<Node> getNodes(Node node, String path) {
        ArrayList nodeList = new ArrayList();

        ArrayList pathList = new ArrayList();
        String[] pathArray = path.split("/");
        for (int i = 0; i < pathArray.length; i++) {
            if (pathArray[i].equals(""))
                continue;
            pathList.add(pathArray[i]);
        }

        for (int i = 0; i < pathList.size(); i++) {
            StringBuffer restPath = new StringBuffer();
            for (int k = i + 1; k < pathList.size(); k++) {
                restPath.append("/").append((String) pathList.get(k));
            }

            for (int j = 0; j < node.getChildNodes().getLength(); j++) {
                if (!node.getChildNodes().item(j).getNodeName().equals(pathList.get(i)))
                    continue;
                if (restPath.length() == 0) {
                    nodeList.add(node.getChildNodes().item(j));
                } else {
                    nodeList.addAll(getNodes(node.getChildNodes().item(j), restPath.toString()));
                }
            }

        }

        return nodeList;
    }
}