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 org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     *  Returns first node at the bottom of path from node.
     * If element begins with '@', indicates an attribute, eg "@id"
     * The '#text' element indicates that the node has a single text child.
     * @param node    Node to apply path to
     * @param path    Path to apply
     * @return        Node at bottom of path, or null
     */
    static public Node extractNode(Node node, String path) {
        if (node == null)
            return null;
        NodeList list = node.getChildNodes();
        if (path.equals("#text"))
            return node.getFirstChild();
        else if (path.charAt(0) == '@')
            return node.getAttributes().getNamedItem(path.substring(1));
        else
            for (int j = 0; j < list.getLength(); j++)
                if (list.item(j).getNodeType() == Node.ELEMENT_NODE && list.item(j).getNodeName().equals(path))
                    return list.item(j);

        return null;
    }
}