Java XML Child Node Get getChildNode(Node root, String childName)

Here you can find the source of getChildNode(Node root, String childName)

Description

Instead of using XPathAPI.selectSingleNode(root, "name")); use getChildNode(root, "name"); This is much quicker!

License

Open Source License

Parameter

Parameter Description
root a parameter
string a parameter

Declaration

public static Node getChildNode(Node root, String childName) 

Method Source Code

//package com.java2s;
/*/*www.  j  a v  a  2 s. c om*/
 * This file is part of smarthomatic, http://www.smarthomatic.org.
 * Copyright (c) 2013 Uwe Freese
 *
 * smarthomatic is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * smarthomatic is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with smarthomatic. If not, see <http://www.gnu.org/licenses/>.
 */

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Instead of using XPathAPI.selectSingleNode(root, "name"));
     * use getChildNode(root, "name"); This is much quicker!
     * @param root
     * @param string
     */
    public static Node getChildNode(Node root, String childName) {
        NodeList nl = root.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            if (nl.item(i).getNodeName().equals(childName)) {
                return nl.item(i);
            }
        }
        System.err.println("Childnode " + childName + " not found!");
        return null;
    }
}

Related

  1. getChildNode(Node node, String nodeName)
  2. getChildNode(Node node, String tag)
  3. getChildNode(Node parent, String childName)
  4. getChildNode(Node parent, String tagName)
  5. getChildNode(Node parentNode, String childElementName)
  6. getChildNode(Node rootNode, String childName)
  7. getChildNodeByLocalName(final Node parentNode, final String localNodeName)
  8. getChildNodeByName(final Node xnode, final String name, final boolean caseSensitive)
  9. getChildNodeByName(Node element, CharSequence nodeName, boolean caseSensitive)