Java XML Element Text getSimpleElementText(final Element element, final String nodeName)

Here you can find the source of getSimpleElementText(final Element element, final String nodeName)

Description

get Simple Element Text

License

Open Source License

Parameter

Parameter Description
element a parameter
nodeName a parameter

Return

String

Declaration

public static String getSimpleElementText(final Element element, final String nodeName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Manchester Centre for Integrative Systems Biology
 * University of Manchester/*from www .j a va2 s.  co  m*/
 * Manchester M1 7ND
 * United Kingdom
 * 
 * Copyright (C) 2007 University of Manchester
 * 
 * This program is released under the Academic Free License ("AFL") v3.0.
 * (http://www.opensource.org/licenses/academic.php)
 *******************************************************************************/

import org.w3c.dom.*;
import org.w3c.dom.Element;

public class Main {
    /**
     * 
     * @param element
     * @param nodeName
     * @return String
     */
    public static String getSimpleElementText(final Element element, final String nodeName) {
        final Element namedElement = getFirstElement(element, nodeName);

        if (namedElement != null) {
            return getSimpleElementText(namedElement);
        }

        return null;
    }

    /**
     * 
     * @param node
     * @return String
     */
    public static String getSimpleElementText(final Node node) {
        final NodeList children = node.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            final Node child = children.item(i);

            if (child instanceof Text) {
                return child.getNodeValue();
            }
        }

        return null;
    }

    /**
     * 
     * @param element
     * @param nodeName
     * @return Element
     */
    public static Element getFirstElement(final Element element, final String nodeName) {
        final NodeList list = element.getElementsByTagName(nodeName);

        if (list.getLength() == 0) {
            return null;
        }

        return (Element) list.item(0);
    }
}

Related

  1. getElementTextValue(Element in)
  2. getSimpleElementText(Element element)
  3. getSimpleElementText(Element node)
  4. getSimpleElementText(Element node)
  5. getSimpleElementText(Element node, boolean trim)