get XML Child Elements - Android XML

Android examples for XML:XML Child Element

Description

get XML Child Elements

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Main {
    public static List<Element> getChildElements(Element parent,
            Enum<?> child) {
        final String elName = getXmlName(child);
        final NodeList list = parent.getElementsByTagName(elName);
        final List<Element> children = new ArrayList<Element>(
                list.getLength());//  w  w  w.ja v  a  2  s .  c o m
        for (int i = 0; i < list.getLength(); i++) {
            final Element elChild = (Element) list.item(i);
            children.add(elChild);
        }
        return children;
    }

    public static String getXmlName(Enum<?> node) {
        String name = node.name();

        //remove leading or trailing underscore, it is used if a name conflicts with a Java keyword
        if (name.length() > 0 && name.charAt(0) == '_') {
            name = name.substring(1);
        }
        if (name.length() > 0 && name.charAt(name.length() - 1) == '_') {
            name = name.substring(0, name.length() - 1);
        }
        return name.replace('_', '-');//use dashes instead of underscores in XML
    }
}

Related Tutorials