Here you can find the source of getChildElementTextValue(Node parent, String name)
Parameter | Description |
---|---|
parent | parent element |
name | name of child element |
public static String getChildElementTextValue(Node parent, String name)
//package com.java2s; /**//w w w .j a va 2 s . com * Copyright 2005-2014 The Kuali Foundation * * Licensed under the Educational Community License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.opensource.org/licenses/ecl2.php * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Returns the text value of a child element with the given name, of the given parent element, * or null if the child does not exist or does not have a child text node * @param parent parent element * @param name name of child element * @return the text value of a child element with the given name, of the given parent element, * or null if the child does not exist or does not have a child text node */ public static String getChildElementTextValue(Node parent, String name) { Element child = getChildElement(parent, name); if (child == null) { return null; } Node textNode = child.getFirstChild(); if (textNode == null) { return null; } return textNode.getNodeValue(); } /** * Returns a node child with the specified tag name of the specified parent node, * or null if no such child node is found. * @param parent the parent node * @param name the name of the child node * @return child node if found, null otherwise */ public static Element getChildElement(Node parent, String name) { NodeList childList = parent.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { Node node = childList.item(i); // we must test against NodeName, not just LocalName // LocalName seems to be null - I am guessing this is because // the DocumentBuilderFactory is not "namespace aware" // although I would have expected LocalName to default to // NodeName if (node.getNodeType() == Node.ELEMENT_NODE && (name.equals(node.getLocalName()) || name .equals(node.getNodeName()))) { return (Element) node; } } return null; } }