Java XML Element Get Value getText(Element element)

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

Description

Get the text that is associated with this element.

License

Open Source License

Parameter

Parameter Description
element an Element object.

Return

the text that is associated with this element.

Declaration

public static String getText(Element element) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2002-2005 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   ww w. j a  v a2 s .  c  o m*/
 *   IBM - Initial API and implementation
 *******************************************************************************/

import org.w3c.dom.CharacterData;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    /**
     * Get the text that is associated with this element.
     * 
     * @param element an Element object.
     * @return the text that is associated with this element.
     */
    public static String getText(Element element) {
        String text = null;

        // Get first child element
        Node node = element.getFirstChild();

        // NodeList nodeList = element.getChildNodes();

        // int length = nodeList.getLength();

        // Process while there are nodes and the text hasn't been found
        while ((node != null) && (text == null)) {
            // If a text node or cdata section is found, then get text
            if ((node.getNodeType() == Node.TEXT_NODE) || (node.getNodeType() == Node.CDATA_SECTION_NODE)) {
                text = ((CharacterData) node).getData();
            }

            // Get next sibling
            node = node.getNextSibling();
        }

        if (text != null)
            text = text.trim();

        return text;
    }

    /**
     * Get the first child element from the input elment.
     * 
     * @param element an Element object.
     * @return the firstchild element.
     */
    public static Element getFirstChild(Element element) {
        // Return the first child element
        return findNextSibling(element.getFirstChild());
    }

    /**
     * Get the next sibling element.
     * 
     * @param element - an Element object.
     * @return the next sibling element.
     */
    public static Element getNextSibling(Element element) {
        // Return next sibling element
        return findNextSibling(element.getNextSibling());
    }

    /**
     * Find the next sibling element.
     * 
     * @param startNode XML start node.
     * @return the next sibling element.
     */
    protected static Element findNextSibling(Node startNode) {
        Node node = null;
        Element returnElement = null;

        // Find the next sibling element
        for (node = startNode; node != null && returnElement == null; node = node.getNextSibling()) {
            // If this node is an element node, then return it
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                returnElement = (Element) node;
            }
        }

        // Return next sibling element
        return (Element) returnElement;
    }
}

Related

  1. getText(Element elem)
  2. getText(Element elem, String ifnull)
  3. getText(Element elem, String name)
  4. getText(Element element)
  5. getText(Element element)
  6. getText(Element element)
  7. getText(Element element)
  8. getText(Element element)
  9. getText(Element element, String name)