Java XML Element Get Value getTextOfElement(Element e)

Here you can find the source of getTextOfElement(Element e)

Description

Returns the content of all text nodes underneath a specified Element, concatenated together into a single string.

License

Mozilla Public License

Parameter

Parameter Description
e The <CODE>Element</CODE> to extract text from.

Return

The text content under this Element node. If the specified Element has no text nodes underneath it, returns null.

Declaration

private static final String getTextOfElement(Element e) 

Method Source Code


//package com.java2s;
/*//  w  w w .j av a2 s.co m
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (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.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002-03 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */

import org.w3c.dom.*;

public class Main {
    /**
     * Returns the content of all text nodes underneath a specified <CODE>Element</CODE>, concatenated
     * together into a single string.
     *
     * @param e The <CODE>Element</CODE> to extract text from.
     * @return The text content under this <CODE>Element</CODE> node.  If the specified <CODE>Element</CODE>
     *         has no text nodes underneath it, returns <CODE>null.</CODE>
     */
    private static final String getTextOfElement(Element e) {
        NodeList kids = e.getChildNodes();
        if (kids == null)
            return null; // no text?

        StringBuffer b = null;
        for (int i = 0; i < kids.getLength(); i++) { // look for an ELEMENT_NODE node matching the desired name
            Node t = kids.item(i);
            if ((t.getNodeType() == Node.TEXT_NODE) || (t.getNodeType() == Node.CDATA_SECTION_NODE)) { // append to the string under construction
                if (b == null)
                    b = new StringBuffer();
                else
                    b.append(' ');
                b.append(t.getNodeValue());

            } // end if

        } // end for

        if (b == null)
            return null; // no TEXT nodes
        else
            return b.toString(); // return the concatenation

    }
}

Related

  1. getTextData(Element element)
  2. getTextElementValue(Element ele)
  3. getTextFromFirstSubEleByName(Element element, String tagName)
  4. getTextFromTag(Element current, String tag)
  5. getTextList(Element elem, String name)
  6. getTexts(Element root, String elementName)
  7. getTextString(Element e)
  8. getTextTrim(Element element)
  9. getTextTrim(Element element)