Look for XML Element node if given name. - Java XML

Java examples for XML:DOM Node

Description

Look for XML Element node if given name.

Demo Code

/*******************************************************************************
 * Copyright (c) 2012 Oak Ridge National Laboratory.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/
//package com.java2s;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /** Look for Element node if given name.
     *  <p>//from  w ww  .j  a va 2  s  . com
     *  Checks the node and its siblings.
     *  Does not descent down the 'child' links.
     *  @param node Node where to start.
     *  @param name Name of the nodes to look for.
     *  @return Returns node or the next matching sibling or null.
     */
    final public static Element findFirstElementNode(Node node,
            final String name) {
        while (node != null) {
            if (node.getNodeType() == Node.ELEMENT_NODE
                    && node.getNodeName().equals(name))
                return (Element) node;
            node = node.getNextSibling();
        }
        return null;
    }
}

Related Tutorials