Java XML Node Find findNode(Document source, Element n)

Here you can find the source of findNode(Document source, Element n)

Description

Find a node in a DOM.

License

LGPL

Parameter

Parameter Description
source The source to look in
n The node were are looking for

Return

The node or null if its not there

Declaration

private static Node findNode(Document source, Element n) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 *                        Copyright Yumetech, Inc (c) 2006
 *                               Java Source
 *
 * This source is licensed under the GNU LGPL v2.1
 * Please read http://www.gnu.org/copyleft/lgpl.html for more information
 *
 * This software comes with the standard NO WARRANTY disclaimer for any
 * purpose. Use it at your own risk. If there's a problem you get to fix it.
 *
 ****************************************************************************/

import org.w3c.dom.*;

public class Main {
    /**//from  w w w. j ava2s .c  om
     * Find a node in a DOM. This is looking for a node which has the same path.
     * Ie /Foo/Bar/MyNode
     *
     * @param source The source to look in
     * @param n The node were are looking for
     * @return The node or null if its not there
     */
    private static Node findNode(Document source, Element n) {
        String name = n.getTagName();
        NodeList list = source.getElementsByTagName(name);

        int len = list.getLength();
        // System.out.println("findNode: " + name + " possibles: " + len);

        if (len == 0)
            return null;

        Element sn;

        for (int i = 0; i < len; i++) {
            sn = (Element) list.item(i);

            if (compareNodeLocation(sn, n))
                return sn;
        }

        return null;
    }

    /**
     * Compare two nodes location in a tree. Returns true if all they have the
     * same XPATH location.
     *
     * @param n1 The first node
     * @param n2 The second node
     * @return true if they have the same location
     */
    private static boolean compareNodeLocation(Element n1, Element n2) {
        // simple check for now, compare parents
        Node p1 = n1.getParentNode();
        Node p2 = n2.getParentNode();

        if (p1 == null && p2 == null)
            return true;
        if (p1 == null && p2 != null)
            return false;
        if (p1 != null && p2 == null)
            return false;

        String name1 = p1.getNodeName();
        String name2 = p2.getNodeName();

        if (name1.equals(name2))
            return true;

        return false;
    }
}

Related

  1. findElementByName(Node node, final String name)
  2. findElementByTag(Set nodes, String tag)
  3. findElementNodeByName(Node node, String name)
  4. findElements(Node fromnode, String name)
  5. findElementsByName(Node node, List elements, String name)
  6. findNode(Node aNode, String aName)
  7. findNode(Node node, String nodeName)
  8. findNode(Node rootNode, String nodeName)
  9. findNodeByAttribute(Document document, String tagName, String attributeName, String attributeValue)