Java XML Element Get getElementByTagName(Element elem, String name, String emptyValue)

Here you can find the source of getElementByTagName(Element elem, String name, String emptyValue)

Description

Get text data of first XML org.w3c.dom.Element of given name.

License

Apache License

Parameter

Parameter Description
elem the parent XML Element
name the name of the child text Element
emptyValue value to return if element exists, but is empty

Return

text data of named child Element

Declaration

private static String getElementByTagName(Element elem, String name, String emptyValue) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

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

public class Main {
    /**/*from  w w  w .  j  ava 2 s. c om*/
     * Get text data of first XML {@code org.w3c.dom.Element} of given name.
     *
     * @param elem the parent XML Element
     * @param name the name of the child text Element
     * @param emptyValue value to return if element exists, but is empty
     * @return text data of named child Element
     */
    private static String getElementByTagName(Element elem, String name, String emptyValue) {
        NodeList nodeList = elem.getElementsByTagName(name);
        if (nodeList.getLength() == 0) {
            return null;
        }

        NodeList children = nodeList.item(0).getChildNodes();
        if (children.getLength() == 0 || children.item(0).getNodeType() != Node.TEXT_NODE) {
            return emptyValue;
        }
        return children.item(0).getNodeValue();
    }
}

Related

  1. getElementByName(Element ele, String name)
  2. getElementByQualifiedName(Element n, String namespace, String elementName)
  3. getElementByTagName(Element current, String name)
  4. getElementByTagName(Element el, String tag)
  5. getElementByTagName(Element el, String tag)
  6. getElementByTagName(Element element, String name)
  7. getElementByTagName(Element element, String name, int index)
  8. getElementByTagName(Element element, String tag)
  9. getElementByTagName(Element element, String tag)