Java XML Element First getFirstElementText(Document document, String tagName)

Here you can find the source of getFirstElementText(Document document, String tagName)

Description

This method returns the first element text from the document.

License

Open Source License

Parameter

Parameter Description
document The document.
tagName The element name.

Return

The text contained in the first element.

Declaration

public static String getFirstElementText(Document document, String tagName) 

Method Source Code

//package com.java2s;
/**//w  ww .j ava 2  s  .  c  om
 * Copyright 2003, 2004  ONCE Corporation
 *
 * LICENSE:
 * This file is part of BuilditMPI. It may be redistributed and/or modified
 * under the terms of the Common Public License, version 1.0.
 * You should have received a copy of the Common Public License along with this
 * software. See LICENSE.txt for details. Otherwise, you may find it online at:
 *   http://www.oncecorp.com/CPL10/ or http://opensource.org/licenses/cpl.php
 *
 * DISCLAIMER OF WARRANTIES AND LIABILITY:
 * THE SOFTWARE IS PROVIDED "AS IS".  THE AUTHOR MAKES NO REPRESENTATIONS OR
 * WARRANTIES, EITHER EXPRESS OR IMPLIED.  TO THE EXTENT NOT PROHIBITED BY LAW,
 * IN NO EVENT WILL THE AUTHOR BE LIABLE FOR ANY DAMAGES, INCLUDING WITHOUT
 * LIMITATION, LOST REVENUE, PROFITS OR DATA, OR FOR SPECIAL, INDIRECT,
 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS
 * OF THE THEORY OF LIABILITY, ARISING OUT OF OR RELATED TO ANY FURNISHING,
 * PRACTICING, MODIFYING OR ANY USE OF THE SOFTWARE, EVEN IF THE AUTHOR HAVE
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * -----------------------------------------------------
 * $Id$
 */

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

public class Main {
    /**
     * This method returns the first element text from the document.
     * @param document The document.
     * @param tagName The element name.
     * @return The text contained in the first element.
     */
    public static String getFirstElementText(Document document, String tagName) {
        Element element = getFirstElement(document, tagName);
        if (element != null) {
            return getText(element);
        } else {
            return null;
        }
    }

    /**
     * This method returns the first element from the document.
     * @param document The document.
     * @param tagName The element name.
     * @return The element object.
     */
    public static Element getFirstElement(Document document, String tagName) {
        NodeList nodes = document.getElementsByTagName(tagName);
        if (nodes != null && nodes.getLength() != 0) {
            return (Element) nodes.item(0);
        } else {
            return null;
        }
    }

    /**
     * Looks for a text child node and returns its value.
     *
     * @param tag - XML element
     * @return - the text String of the tag
     */
    public static String getText(final Element tag) {
        if (tag == null)
            return null;

        NodeList lst = tag.getChildNodes();
        StringBuffer buf = new StringBuffer();

        for (int Index = 0, Cnt = lst.getLength(); Index < Cnt; Index++) {
            if (lst.item(Index).getNodeType() == Node.ENTITY_REFERENCE_NODE) {
                buf.append(lst.item(Index).getChildNodes().item(0).getNodeValue());
            }
            if ((lst.item(Index).getNodeType() == Node.TEXT_NODE)
                    || (lst.item(Index).getNodeType() == Node.CDATA_SECTION_NODE)) {
                buf.append(lst.item(Index).getNodeValue());
            }
        }

        if (buf.length() == 0)
            return null;
        else
            return buf.toString();
    }
}

Related

  1. getFirstElement(Element documentElement)
  2. getFirstElement(String name, Element root)
  3. getFirstElementByTagName(Document doc, String tagName)
  4. getFirstElementByTagName(Document document, String name)
  5. getFirstElementByTagName(Document document, String tag)
  6. getFirstImageTransformElement(final Document doc)
  7. getFirstNode(Document doc, String xPathExpression)
  8. getFirstNodeByName(Document d, String s)