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

ListgetElementsByTagNameCaseInsensitive(Document doc, final Set lowerCaseNames)
get Elements By Tag Name Case Insensitive
final List<Element> result = Lists.newArrayList();
NodeIterator nodeIterator = ((DocumentTraversal) doc).createNodeIterator(doc, NodeFilter.SHOW_ELEMENT,
        new NodeFilter() {
            public short acceptNode(Node n) {
                if (lowerCaseNames.contains(n.getNodeName().toLowerCase())) {
                    return NodeFilter.FILTER_ACCEPT;
                return NodeFilter.FILTER_REJECT;
...
StringgetElementValue(Element parent)
get Element Value
NodeList nodes = parent.getChildNodes();
int current = 0;
int length = nodes.getLength();
while (current < length) {
    Node node = nodes.item(current);
    if (node instanceof Text) {
        String value = node.getNodeValue();
        if (value != null) {
...
StringgetElementValue(Element parent, String tagName)
Gets the value of the child element by tag name under the given parent element.
String value = null;
Element element = getElement(parent, tagName);
if (element != null) {
    value = element.getTextContent();
return value;
StringgetElementValue(Element parent, String tagName)
get Element Value
Element element = getChildElement(parent, tagName);
if (element != null) {
    NodeList nodes = element.getChildNodes();
    if ((nodes != null) && (nodes.getLength() > 0)) {
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if ((node instanceof Text)) {
                return ((Text) node).getData();
...
ElementgetFirstElement(Element parent, String name)
get the first element in the parent element that's named name or null if no such element exists
NodeList list = parent.getElementsByTagName(name);
Node node = list.item(0);
if (node == null) {
    return null;
if (!isElement(node)) {
    throw new RuntimeException("XML Parsing error: " + name + " is not an Element");
return (Element) node;
ElementgetFirstElementAtAnyDepthByNodeName(Element parent, String name)
Find the first element that is a child or subchild under a given Element parent.
NodeList l = parent.getChildNodes();
int count = l.getLength();
for (int i = 0; i < count; i++) {
    Node n = l.item(i);
    if (n.getNodeType() == Node.ELEMENT_NODE) {
        Element e = (Element) n;
        String nodeName = e.getNodeName();
        if (name.equals(nodeName)) {
...
ElementgetFirstElementByName(Element parent, String name)
Find a element by name.
NodeList l = parent.getChildNodes();
int count = l.getLength();
for (int i = 0; i < count; i++) {
    Node n = l.item(i);
    if (n.getNodeType() == Node.ELEMENT_NODE) {
        Element e = (Element) n;
        String localName = e.getLocalName();
        if (localName == null) {
...
ElementgetFirstElementByName(String name, Element parent)
get First Element By Name
return (Element) parent.getElementsByTagName(name).item(0);
ElementgetFirstElementByTagName(Element parent, String tagName)
get First Element By Tag Name
List<Element> foundElements = getNamedChildElements(parent, tagName);
if (foundElements.size() > 0) {
    return foundElements.get(0);
} else {
    return null;
ElementgetGrandSonElementByTagName(Element element, String parentName, String eleName)
get Grand Son Element By Tag Name
NodeList nl = element.getElementsByTagName(parentName);
if (null == nl) {
    return null;
Node item = nl.item(0);
return getChildElementByTagName((Element) item, eleName);