Skip to content

Instantly share code, notes, and snippets.

@VenkataRaju
Last active August 29, 2015 14:07
Show Gist options
  • Save VenkataRaju/973a05e2b84def542a14 to your computer and use it in GitHub Desktop.
Save VenkataRaju/973a05e2b84def542a14 to your computer and use it in GitHub Desktop.
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public final class XmlUtil
{
/** Returns all descendant Elements with a given tag name, in document order. */
public static List<Element> getElementByTagName(Document doc, String childTag)
{
return getElementByTagName(doc.getDocumentElement(), childTag);
}
/** Same as {@link #getElementsByTagName(Document, String)} */
public static List<Element> getElementByTagName(Element el, String childTag)
{
final NodeList nodeList = el.getElementsByTagName(childTag);
return new AbstractList<Element>()
{
private final int size = nodeList.getLength();
@Override
public Element get(int index)
{
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("index: " + index + ", size: " + size);
return (Element) nodeList.item(index);
}
@Override
public int size()
{
return size;
}
};
}
public static Iterable<Node> asIterable(NodeList nodeList)
{
return new Iterable<Node>()
{
@Override
public Iterator<Node> iterator()
{
return new Iterator<Node>()
{
final int size = nodeList.getLength();
int index;
@Override
public boolean hasNext()
{
return index < size;
}
@Override
public Node next()
{
if (!hasNext())
throw new NoSuchElementException();
return nodeList.item(index);
}
};
}
};
}
/**
* @param exactDepth
* should be > 0
*/
public static List<Element> getElementsByTagName(Document doc, String tagName, int exactDepth)
{
return getElementsByTagName(doc.getDocumentElement(), tagName, exactDepth);
}
/**
* @param exactDepth
* should be > 0
*/
public static List<Element> getElementsByTagName(Element el, String tagName, int exactDepth)
{
if (exactDepth < 1)
throw new IllegalArgumentException("Invalid depth: " + exactDepth);
List<Element> elements = new ArrayList<Element>();
updateElementsByTagName(el, tagName, 1, exactDepth, elements);
return elements;
}
private static void updateElementsByTagName(Element el, String tagName, int currentDepth, int targetDepth, List<Element> elements)
{
Iterable<Node> childNodes = asIterable(el.getChildNodes());
if (currentDepth < targetDepth)
{
int cd = currentDepth + 1;
for (Node node : childNodes)
if (node.getNodeType() == Node.ELEMENT_NODE)
updateElementsByTagName((Element) node, tagName, cd, targetDepth, elements);
}
else
{
for (Node node : childNodes)
if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(tagName))
elements.add((Element) node);
}
}
public static String getFirstChildElementContent(Element el, String childTag)
{
return el.getElementsByTagName(childTag).item(0).getTextContent();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment