Here you can find the source of getElementData(final Element root, final String elementName)
Parameter | Description |
---|---|
root | the root of the element whose text is to be retrieved; assumed not to be <code>null</code>. |
elementName | the name of the element whose text is to be retrieved. |
public static String getElementData(final Element root, final String elementName)
//package com.java2s; /*/*w w w.ja va 2s. c o m*/ =========================================================================== Copyright (c) 2010 BrickRed Technologies Limited Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sub-license, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. =========================================================================== */ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Static helper function to get the element data of the specified node. * * @param node * the node where the text data resides; may be <code>null</code> * in which case this funtion will return "" * * @return the complete text of the specified node, or an empty string if * the node has no text or is <code>null</code> */ public static String getElementData(final Node node) { StringBuffer ret = new StringBuffer(); if (node != null) { Node text; for (text = node.getFirstChild(); text != null; text = text .getNextSibling()) { /** * the item's value is in one or more text nodes which are its * immediate children */ if (text.getNodeType() == Node.TEXT_NODE || text.getNodeType() == Node.CDATA_SECTION_NODE) { ret.append(text.getNodeValue()); } else { if (text.getNodeType() == Node.ENTITY_REFERENCE_NODE) { ret.append(getElementData(text)); } } } } return ret.toString(); } /** * Gets the text value of the specified element. * * @param root * the root of the element whose text is to be retrieved; assumed * not to be <code>null</code>. * @param elementName * the name of the element whose text is to be retrieved. * * @return the retrieved text value of the specified element or null if root * has no given element. */ public static String getElementData(final Element root, final String elementName) { NodeList nodes = root.getElementsByTagName(elementName); if (nodes.getLength() < 1) { return null; } return getElementData(nodes.item(0)); } }