Java Utililty Methods XML Element Children

List of utility methods to do XML Element Children

Description

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

Method

StringgetSubelementString(final Element element, final String name)
Locate a sub-element tagged 'name', return its value.
if (element == null)
    return "";
Node n = element.getFirstChild();
n = findFirstElementNode(n, name);
if (n != null) {
    Node text_node = n.getFirstChild();
    if (text_node == null)
        return "";
...
StringgetSubelementString(final Element element, final String name)
Locate a sub-element tagged 'name', return its value.
if (element == null)
    throw new Exception("Missing '" + name + "'");
Node n = element.getFirstChild();
n = findFirstElementNode(n, name);
if (n == null)
    throw new Exception("Missing '" + name + "'");
final Node text_node = n.getFirstChild();
if (text_node == null)
...
StringgetSubelementString(final Element element, final String name, final String default_value)
Locate a sub-element tagged 'name', return its value.
if (element == null)
    return default_value;
Node n = element.getFirstChild();
n = findFirstElementNode(n, name);
return getString(n, default_value);
String[]getSubelementStrings(final Element element, final String name)
Locate all sub-elements tagged 'name', return their values.
ArrayList<String> values = null;
if (element != null) {
    Element n = findFirstElementNode(element.getFirstChild(), name);
    while (n != null) {
        if (values == null)
            values = new ArrayList<String>();
        values.add(n.getFirstChild().getNodeValue());
        n = findNextElementNode(n, name);
...
StringgetSubelementText(Element element, String subelementName)
Get the text value of the element's subelement
if (element != null) {
    NodeList subelementList = element.getElementsByTagName(subelementName);
    if (subelementList != null && subelementList.getLength() > 0) {
        for (int i = 0; i < subelementList.getLength(); i++) {
            Element subElement = (Element) subelementList.item(0);
            if (subElement != null && subElement.getParentNode() == element) {
                return subElement.getNodeValue();
return null;
StringgetSubNodeText(Element modsroot, String nodename, String subnodename)
Retrieves name/subname text value.
StringBuffer sb = new StringBuffer("");
NodeList list = modsroot.getElementsByTagName(nodename);
if (list.getLength() > 0) {
    Element node = (Element) list.item(0);
    NodeList l = node.getElementsByTagName(subnodename);
    if (l.getLength() > 0)
        sb.append(getTextValue(l.item(0)));
return sb.toString();
StringgetSubNodeValue(Element element, String name)
get Sub Node Value
NodeList nodeList = element.getElementsByTagName(name);
return getNodeValue(nodeList.item(0)).trim();