Java XML Node Text Value getSimpleElementText(final Node node)

Here you can find the source of getSimpleElementText(final Node node)

Description

get Simple Element Text

License

Open Source License

Parameter

Parameter Description
node a parameter

Return

String

Declaration

public static String getSimpleElementText(final Node node) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Manchester Centre for Integrative Systems Biology
 * University of Manchester/*from   w  w  w.j  a v a  2s.c  om*/
 * 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. getPlainTextBelow(Node n)
  2. getPropertiesFromXML(Node propNode)
  3. getPropertiesFromXML(Node propNode)
  4. getPropText(Node node)
  5. getRawContent(Node n)
  6. getSimpleNodeValue(Node node)
  7. getSingleNodeTextContent(Node node, String path)
  8. getStrElementValue(Node node)
  9. getString(final Node node, final String default_value)