Java XML Child Node Get getChildNodeValue(Node root, String childName, boolean emptyStringOnMissingChild)

Here you can find the source of getChildNodeValue(Node root, String childName, boolean emptyStringOnMissingChild)

Description

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

License

Open Source License

Parameter

Parameter Description
root a parameter
string a parameter

Declaration

public static String getChildNodeValue(Node root, String childName,
        boolean emptyStringOnMissingChild) 

Method Source Code

//package com.java2s;
/*// w  ww.  ja va 2  s . co m
 * 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 getChildNodeValue(root, "name"); This is much quicker!
     * @param root
     * @param string
     */
    public static String getChildNodeValue(Node root, String childName,
            boolean emptyStringOnMissingChild) {
        NodeList nl = root.getChildNodes();

        for (int i = 0; i < nl.getLength(); i++) {
            if (nl.item(i).getNodeName().equals(childName)) {
                Node textNode = nl.item(i).getFirstChild();

                if (textNode == null)
                    return "";
                else
                    return textNode.getNodeValue();
            }
        }

        if (emptyStringOnMissingChild) {
            return "";
        } else {
            System.err.println("Childnode " + childName + " not found!");
            return null;
        }
    }

    public static String getChildNodeValue(Node root, String childName) {
        return getChildNodeValue(root, childName, false);
    }
}

Related

  1. getChildNodeTextContent(Node parentNode, String childElementName)
  2. getChildNodeTextContents(Node node, String name)
  3. getChildNodeValue(Element node, String tagName)
  4. getChildNodeValue(Element root, String childNodeName, String defaultValue)
  5. getChildNodeValue(Node node, String elementName)
  6. getChildNodeValue(String childNodeName, Node n)
  7. getChildNodeValueByName(Node parent, String nodeName)
  8. getChildNodeWithName(Node node, String name, String namespaceURI)
  9. getChildNodeWithName(String name, Node n)