Java XML Node Text Value getText(final Node xmlNode)

Here you can find the source of getText(final Node xmlNode)

Description

Retrieves contents of first Text Node defined within an XML Node

License

Open Source License

Parameter

Parameter Description
xmlNode parsed XML Node in which to look for a Text Node

Return

text character string value of text node

Declaration

public final static String getText(final Node xmlNode) 

Method Source Code

//package com.java2s;
/**/*w ww. ja v a2s. co  m*/
 * The contents of this file are subject to the Regenstrief Public License
 * Version 1.0 (the "License"); you may not use this file except in compliance with the License.
 * Please contact Regenstrief Institute if you would like to obtain a copy of the license.
 *
 * 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.
 *
 * Copyright (C) Regenstrief Institute.  All Rights Reserved.
 */

import org.w3c.dom.*;

public class Main {
    /**
     * Retrieves contents of first Text Node defined within an XML Node
     * 
     * @param xmlNode parsed XML Node in which to look for a Text Node
     * @return text character string value of text node
     **/
    public final static String getText(final Node xmlNode) {
        return xmlNode == null ? null : xmlFindTextNode(xmlNode);
    }

    /**
     * returns contents of first TextNode defined within an XML Node
     * 
     * @param xmlNode parsed XML Node in which to look for a TextNode
     * @return text character string value of text node
     **/
    public static String xmlFindTextNode(final Node xmlNode) {
        if (xmlNode == null) {
            return null;
        }

        final NodeList children = xmlNode.getChildNodes();
        final int childrenCnt = size(children);
        for (int j = 0; j < childrenCnt; j++) {
            final Node childNode = children.item(j);
            if ((childNode != null) && (childNode.getNodeType() == Node.TEXT_NODE)) {
                return childNode.getNodeValue();
            }
        }
        return null;
    }

    /**
     * Retrieves the number of Nodes in the NodeList
     * 
     * @param nodeList the NodeList
     * @return the number of Nodes
     **/
    public final static int size(final NodeList nodeList) {
        return nodeList == null ? 0 : nodeList.getLength();
    }

    /**
     * Retrieves the number of Nodes in the NamedNodeMap
     * 
     * @param nodeMap the NamedNodeMap
     * @return the number of Nodes
     **/
    public final static int size(final NamedNodeMap nodeMap) {
        return nodeMap == null ? 0 : nodeMap.getLength();
    }

    /**
     * Retrieves the number of Nodes in the tree rooted at the given Node
     * 
     * @param node the root Node
     * @return the number of Nodes
     **/
    public final static int size(final Node node) {
        if (node == null) {
            return 0;
        }

        final NodeList children = node.getChildNodes();
        int size = 1;
        for (int i = 0, n = size(children); i < n; i++) {
            size += size(children.item(i));
        }

        return size;
    }
}

Related

  1. getStringValue(Node node)
  2. getStringValue(Node node)
  3. getTagValue(Node node, String name)
  4. getTagValueWithoutNamespace(Node n)
  5. getText(final Node node)
  6. getText(Node elem)
  7. getText(Node elem, StringBuilder buffer)
  8. getText(Node n)
  9. getText(Node n)