Java XML Element Root getElementData(final Element root, final String elementName)

Here you can find the source of getElementData(final Element root, final String elementName)

Description

Gets the text value of the specified element.

License

Artistic License

Parameter

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.

Return

the retrieved text value of the specified element or null if root has no given element.

Declaration

public static String getElementData(final Element root, final String elementName) 

Method Source Code

//package com.java2s;
//License from project: Artistic License 

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

public class Main {
    /**/*from  w  w w.j  a va2s. c  om*/
     * 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));
    }
}

Related

  1. createRootElement(Document doc, String name)
  2. createRootElement(Document doc, String name)
  3. createRootElement(Document doc, String rootTagName)
  4. getElement(String elementName, Node rootNode)
  5. getElementData(final Element root, final String elementName)
  6. getElementText(String elemName, Element root, boolean trim)
  7. getElementValue(Element root, String name)
  8. getElementValueLong(Element root, String name)
  9. getRoot(Document doc)