Java XML Element Get Value getTextValue(Element ele, String tagName)

Here you can find the source of getTextValue(Element ele, String tagName)

Description

Returns text value of first element in specified element with specified tag.

License

Open Source License

Parameter

Parameter Description
ele Dom element
tagName name of xml tag

Return

text value of element with specified xml tag

Declaration

public static String getTextValue(Element ele, String tagName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2011 The University of Memphis.  All rights reserved. 
 * This program and the accompanying materials are made available 
 * under the terms of the LIDA Software Framework Non-Commercial License v1.0 
 * which accompanies this distribution, and is available at
 * http://ccrg.cs.memphis.edu/assets/papers/2010/LIDA-framework-non-commercial-v1.0.pdf
 *******************************************************************************/

import java.util.ArrayList;

import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

public class Main {
    /**/*from   ww  w  .  ja  va2  s.  com*/
     * Returns text value of first element in specified element with specified
     * tag. Returns null if specified tag is empty or only has subchildren.
     * 
     * @param ele
     *            Dom element
     * @param tagName
     *            name of xml tag
     * @return text value of element with specified xml tag
     */
    public static String getTextValue(Element ele, String tagName) {
        String textVal = null;
        List<Element> nl = getChildren(ele, tagName);
        if (nl != null && nl.size() != 0) {
            Element el = nl.get(0);
            textVal = getValue(el);
            if (textVal != null) {
                textVal = textVal.trim();
                if (textVal.equalsIgnoreCase("")) {
                    return null;
                }
            }
        }

        return textVal;
    }

    /**
     * Returns all children with specified name.
     * 
     * @param parent
     *            {@link Element}
     * @param name
     *            name of sought children
     * @return list of all children found.
     */
    public static List<Element> getChildren(Element parent, String name) {
        List<Element> nl = new ArrayList<Element>();
        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (child instanceof Element && name.equals(child.getNodeName())) {
                nl.add((Element) child);
            }
        }
        return nl;
    }

    /**
     * Returns String value of first child found that is a {@link Text}. or null
     * if the Text is empty
     * 
     * @param parent
     *            Element
     * @return child's string value.
     */
    public static String getValue(Element parent) {
        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (child instanceof Text) {
                String value = child.getNodeValue().trim();
                if (value.equalsIgnoreCase("")) {
                    return null;
                } else {
                    return value;
                }
            }
        }
        return null;
    }
}

Related

  1. getTextString(Element e)
  2. getTextTrim(Element element)
  3. getTextTrim(Element element)
  4. getTextValue(Element el, String tagName)
  5. getTextValue(Element ele, String tagName)
  6. getTextValue(Element ele, String tagName)
  7. getTextValue(Element ele, String tagName)
  8. getTextValue(Element ele, String tagName)
  9. getTextValue(Element ele, String tagName)