Java Utililty Methods XML Element Get Value

List of utility methods to do XML Element Get Value

Description

The list of methods to do XML Element Get Value are organized into topic(s).

Method

StringgetTextContent(Element element)
get Text Content
return org.springframework.util.xml.DomUtils.getTextValue(element);
StringgetTextContent(Element element)
Obtain the text content of an element.
return getTextContent(element, true);
StringgetTextContentFromFirstElementByTagName(Element element, String tagName)
get Text Content From First Element By Tag Name
return getTrimmedTextContent(getFirstElementByTagName(element, tagName));
StringgetTextContentOfElement(Element elem, String tagName, boolean required)
get Text Content Of Element
List<String> result = getTextContentOfElements(elem, tagName);
if (result.isEmpty() && !required)
    return null;
if (result.size() != 1)
    throw new IOException("expected exactly one " + tagName + " element");
return result.get(0);
ListgetTextContentOfElements(Element elem, String tagName)
get Text Content Of Elements
List<String> result = new ArrayList<String>();
NodeList inputElems = elem.getElementsByTagName(tagName);
for (int i = 0; i < inputElems.getLength(); i++) {
    Element childElem = (Element) inputElems.item(i);
    result.add(childElem.getTextContent());
return result;
StringgetTextContents(Element e)
get Text Contents
if (!e.hasChildNodes())
    return null;
StringBuffer buf = new StringBuffer();
NodeList list = e.getChildNodes();
int len = list.getLength();
String text;
for (int i = 0; i < len; i++) {
    text = ((Node) list.item(i)).getNodeValue();
...
StringgetTextData(Element element)
get Text Data
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
    if ((childNodes.item(i).getNodeType() == Node.TEXT_NODE)
            || (childNodes.item(i).getNodeType() == Node.CDATA_SECTION_NODE)) {
        if (childNodes.item(i) != null) {
            String tmpStr = ((Text) childNodes.item(i)).getWholeText().trim();
            if (tmpStr.length() > 0) {
                return tmpStr;
...
StringgetTextElementValue(Element ele)
get Text Element Value
StringBuffer buffer = new StringBuffer();
Node node = ele.getFirstChild();
while (node != null) {
    if (node.getNodeType() == Node.TEXT_NODE) {
        buffer.append(node.getNodeValue());
    } else if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
        buffer.append(node.getNodeValue());
    node = node.getNextSibling();
return buffer.toString();
StringgetTextFromFirstSubEleByName(Element element, String tagName)
get Text From First Sub Ele By Name
List<Element> childEle = getSubElementByName(element, tagName);
for (int i = 0; i < childEle.size(); i++) {
    String tmp = getTextData(childEle.get(i));
    if (tmp != null)
        return tmp;
return null;
StringgetTextFromTag(Element current, String tag)
Return the inner text from the given tag
if (current == null)
    return "";
String result = "";
NodeList nodes = current.getElementsByTagName(tag);
if (nodes.getLength() > 0)
    result = ((Element) nodes.item(0)).getTextContent();
return result;