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

Elementclear(Element node, String subElement)
clear
Element child = findFirst(node, subElement);
if (child == null) {
    child = node.getOwnerDocument().createElement(subElement);
    node.appendChild(child);
child.setTextContent(null); 
return child;
ListfindAll(Element node, String subElement)
Returns all 'subElement' of 'node'.
if (node != null) {
    NodeList nl = node.getElementsByTagName(subElement);
    if (nl != null && nl.getLength() > 0) {
        int n = nl.getLength();
        ArrayList<Element> ans = new ArrayList<Element>(n);
        for (int i = 0; i < n; ++i)
            ans.add((Element) nl.item(i));
        return ans;
...
ElementgetFirstSubElement(Element element)
get First Sub Element
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
    switch (childNodes.item(i).getNodeType()) {
    case Node.ELEMENT_NODE:
        return (Element) childNodes.item(i);
return null;
...
ListgetImmediateSubElements(Element element, String name)
get Immediate Sub Elements
NodeList nl = element.getChildNodes();
List<Element> els = new ArrayList<Element>();
for (int i = 0; i < nl.getLength(); i++) {
    Node n = nl.item(i);
    if (n == null || n.getNodeType() != Node.ELEMENT_NODE)
        continue;
    if (!n.getNodeName().equals(name))
        continue;
...
StringgetRelationUri(Element subject, Element object, String defaultBaseUri)
get Relation Uri
return getSchemaUri(subject, defaultBaseUri) + "." + object.getNodeName();
ElementgetSubElement(Element ele, String tagName)
get Sub Element
NodeList list = ele.getElementsByTagName(tagName);
if (list.getLength() > 0) {
    return (Element) list.item(0);
} else
    return null;
StringgetSubElement(Element element, String subTagName)
get Sub Element
return getSubElement(element, subTagName, 0);
booleangetSubelementBoolean(final Element element, final String element_name, final boolean default_value)
Locate a sub-element tagged 'name', return its boolean value.
final String s = getSubelementString(element, element_name);
if (s.equalsIgnoreCase("true"))
    return true;
if (s.equalsIgnoreCase("false"))
    return false;
return default_value;
doublegetSubelementDouble(final Element element, final String element_name)
Locate a sub-element tagged 'name', return its double value.
final String s = getSubelementString(element, element_name);
try {
    return Double.parseDouble(s);
} catch (NumberFormatException ex) {
    throw new Exception("Missing numeric value for '" + element_name + "'");
doublegetSubelementDouble(final Element element, final String name, final double default_value)
Locate a sub-element tagged 'name', return its value.
final String text = getSubelementString(element, name, "").trim();
if (text.isEmpty())
    return default_value;
try {
    return Double.parseDouble(text);
} catch (NumberFormatException ex) {
    throw new Exception("Invalid number for <" + name + ">", ex);