Java Utililty Methods XML Element Get by Name

List of utility methods to do XML Element Get by Name

Description

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

Method

ListgetGrandSonElementsByTagName(Element ele, String parentName, String eleName)
get Grand Son Elements By Tag Name
NodeList nl = ele.getElementsByTagName(parentName);
if (null == nl) {
    return null;
Node item = nl.item(0);
if (null == item) {
    return null;
NodeList subNodeList = item.getChildNodes();
List<Node> childEles = new ArrayList<Node>();
Node node = null;
for (int i = 0; i < subNodeList.getLength(); i++) {
    node = subNodeList.item(i);
    if (node != null) {
        if (node instanceof Element && eleName.equals(node.getNodeName())
                || eleName.equals(node.getLocalName())) {
            childEles.add(node);
return childEles;
StringgetGrandSonElementValueByTagName(Element element, String parentName, String eleName)
get Grand Son Element Value By Tag Name
NodeList nl = element.getElementsByTagName(parentName);
if (null == nl) {
    return null;
Node item = nl.item(0);
return getChildElementValueByTagName((Element) item, eleName);
ListgetGrandSonListValueByTagName(Element element, String parentName, String eleName)
get Grand Son List Value By Tag Name
NodeList nl = element.getElementsByTagName(parentName);
if (null == nl) {
    return null;
Node item = nl.item(0);
if (null == item) {
    return null;
NodeList subNodeList = item.getChildNodes();
List<String> childEles = new ArrayList<String>();
Node node = null;
for (int i = 0; i < subNodeList.getLength(); i++) {
    node = subNodeList.item(i);
    if (node != null) {
        if (node instanceof Element && eleName.equals(node.getNodeName())
                || eleName.equals(node.getLocalName())) {
            childEles.add(getTextValue((Element) node));
return childEles;
StringgetValue(Document doc, String Tag)
Get Value from XML
if (doc.getElementsByTagName(Tag) == null)
    return "";
if (doc.getElementsByTagName(Tag).item(0) == null)
    return "";
return doc.getElementsByTagName(Tag).item(0).getTextContent();
StringgetValue(Document doc, String tagName)
get Value
return getValue(doc.getDocumentElement(), tagName);
StringgetValue(Document pDocument, String psTagName)
returns a XML node value.
String s = null;
try {
    NodeList elements = pDocument.getDocumentElement().getElementsByTagName(psTagName);
    Node node = elements.item(0);
    NodeList nodes = node.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        s = ((Node) nodes.item(i)).getNodeValue().trim();
        if (s.equals("") || s.equals("\r"))
...
NodegetXMLElement(Document document, String name)
get XML Element
if (document == null)
    return null;
NodeList list = document.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
    Node node = list.item(i);
    if (node.getNodeType() != Node.ELEMENT_NODE)
        continue;
    if (name.equals(node.getNodeName()))
...