Android XML Element Value Get getText(Element parentNode)

Here you can find the source of getText(Element parentNode)

Description

Extracts the String content of a TXT element.

License

LGPL

Parameter

Parameter Description
parentNode the node containing the data that we'd like to get.

Return

the string contained by the node or null if none existed.

Declaration

public static String getText(Element parentNode) 

Method Source Code

//package com.java2s;
/*/*  ww w  . ja  v  a2 s.  c o m*/
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

import org.w3c.dom.*;

public class Main {
    /**
     * Extracts the String content of a TXT element.
     *
     * @param parentNode the node containing the data that we'd like to get.
     * @return the string contained by the node or null if none existed.
     */
    public static String getText(Element parentNode) {
        Text text = getTextNode(parentNode);

        return (text == null) ? null : text.getData();
    }

    /**
     * Returns element's TEXT child node (if it has one).
     * @param element the element whose TEXT we need to get.
     * @return a <tt>Text</tt> object containing the specified element's
     * text content.
     */
    public static Text getTextNode(Element element) {
        return (Text) getChildByType(element, Node.TEXT_NODE);
    }

    /**
     * Returns first of the <tt>element</tt>'s child nodes that is of type
     * <tt>nodeType</tt>.
     * @param element the element whose child we need.
     * @param nodeType the type of the child we need.
     * @return a child of the specified <tt>nodeType</tt> or null if none
     * was found.
     */
    public static Node getChildByType(Element element, short nodeType) {
        if (element == null)
            return null;

        NodeList nodes = element.getChildNodes();
        if (nodes == null || nodes.getLength() < 1)
            return null;

        Node node;
        String data;
        for (int i = 0; i < nodes.getLength(); i++) {
            node = nodes.item(i);
            short type = node.getNodeType();
            if (type == nodeType) {
                if (type == Node.TEXT_NODE
                        || type == Node.CDATA_SECTION_NODE) {
                    data = ((Text) node).getData();
                    if (data == null || data.trim().length() < 1)
                        continue;
                }

                return node;
            }
        }

        return null;
    }
}

Related

  1. getNodeValue(Element node)
  2. getNodeValue(Element node)
  3. getTextNode(Element element)
  4. getTextTrim(Element elto)
  5. getTextTrim(Element elto)
  6. getValue(Element element)