Here you can find the source of getChildNodeValue(Node root, String childName, boolean emptyStringOnMissingChild)
Parameter | Description |
---|---|
root | a parameter |
string | a parameter |
public static String getChildNodeValue(Node root, String childName, boolean emptyStringOnMissingChild)
//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); } }