Android Utililty Methods XML Element Child Get

List of utility methods to do XML Element Child Get

Description

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

Method

StringgetFullTextChildrenFromElement(Element element)
Method getFullTextChildrenFromElement
StringBuilder sb = new StringBuilder();
Node child = element.getFirstChild();
while (child != null) {
    if (child.getNodeType() == Node.TEXT_NODE) {
        sb.append(((Text) child).getData());
    child = child.getNextSibling();
return sb.toString();
voidprintChildElements(Element root, PrintStream out, boolean recurse, String prefix)
Whenever you'd need to print a configuration node and/or its children.
out.print(prefix + "<" + root.getNodeName());
NamedNodeMap attrs = root.getAttributes();
Node node;
for (int i = 0; i < attrs.getLength(); i++) {
    node = attrs.item(i);
    out.print(" " + node.getNodeName() + "=\""
            + node.getNodeValue() + "\"");
out.println(">");
String data = getText(root);
if (data != null && data.trim().length() > 0)
    out.println(prefix + "\t" + data);
data = getCData(root);
if (data != null && data.trim().length() > 0)
    out.println(prefix + "\t<![CDATA[" + data + "]]>");
NodeList nodes = root.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
    node = nodes.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        if (recurse)
            printChildElements((Element) node, out, recurse, prefix
                    + "\t");
        else
            out.println(prefix + node.getNodeName());
out.println(prefix + "</" + root.getNodeName() + ">");
StringvalueOfFirstChildWithName(Element element, String name)
value Of First Child With Name
Element e = firstChildWithName(element, name);
if (e != null) {
    Node firstChild = e.getFirstChild();
    if (firstChild != null)
        return firstChild.getNodeValue();
return null;
StringattributeOfFirstChildWithName(Element element, String name, String attribute)
attribute Of First Child With Name
Element e = firstChildWithName(element, name);
if (element != null) {
    return e.getAttribute(attribute);
return null;
ListchildrenWithName(Element e, String name)
children With Name
final List<Element> ret = new ArrayList<Element>();
NodeList list = e.getChildNodes();
final int length = list.getLength();
for (int i = 0; i < length; ++i) {
    Node n = list.item(i);
    if (n.getNodeType() == Node.ELEMENT_NODE
            && n.getNodeName().equals(name))
        ret.add((Element) n);
...
CDATASectiongetCDataNode(Element element)
Returns element's CDATA child node (if it has one).
return (CDATASection) getChildByType(element,
        Node.CDATA_SECTION_NODE);
ListlocateElements(Element root, String tagName, String keyAttributeName, String keyAttributeValue)
Looks through all child elements of the specified root (recursively) and returns the elements that corresponds to all parameters.
List<Element> result = new ArrayList<Element>();
NodeList nodes = root.getChildNodes();
Node node;
int len = nodes.getLength();
for (int i = 0; i < len; i++) {
    node = nodes.item(i);
    if (node.getNodeType() != Node.ELEMENT_NODE)
        continue;
...
ElementlocateElement(Element root, String tagName, String keyAttributeName, String keyAttributeValue)
Looks through all child elements of the specified root (recursively) and returns the first element that corresponds to all parameters.
NodeList nodes = root.getChildNodes();
int len = nodes.getLength();
for (int i = 0; i < len; i++) {
    Node node = nodes.item(i);
    if (node.getNodeType() != Node.ELEMENT_NODE)
        continue;
    Element element = (Element) node;
    if (node.getNodeName().equals(tagName)) {
...
StringvalueOfFirstDescendantWithName(Element element, String name)
value Of First Descendant With Name
Element e = firstDescendantWithName(element, name);
if (e != null) {
    return e.getFirstChild().getNodeValue();
return null;
ElementfirstDescendantWithName(Element element, String name)
first Descendant With Name
NodeList list = element.getElementsByTagName(name);
if (list.getLength() > 0) {
    Node n = list.item(0);
    if (n.getNodeType() == Node.ELEMENT_NODE)
        return (Element) n;
return null;