Java Utililty Methods XML Element Get

List of utility methods to do XML Element Get

Description

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

Method

ElementfindElement(Document doc, String elementNS, String elementName, String attrName, String attrValue)
find Element
return findElement(doc.getDocumentElement(), elementNS, elementName, attrName, attrValue);
ElementfindElement(Document doc, String tagName, Properties props)
Find an element
Element elmt = null;
NodeList nlist = doc.getElementsByTagName(tagName);
for (int i = 0; i < nlist.getLength() && elmt == null; i++) {
    Node node = nlist.item(i);
    if (node instanceof Element) {
        Element temp = (Element) node;
        boolean matches = true;
        for (Enumeration en = props.keys(); en.hasMoreElements() && matches;) {
...
ElementfindElement(String name, Document doc)
Finds the first (breadth first) DOM element with the specified name.
return findElement(name, null, null, doc);
ElementfindElementAndSetElseCreateAndSet(Document document, Element parent, String child, boolean value)
find Element And Set Else Create And Set
return findElementAndSetElseCreateAndSet(document, parent, child, "" + value);
ElementfindElementElseCreateAndSet(Document document, Element parent, String child, boolean value)
find Element Else Create And Set
return findElementElseCreateAndSet(document, parent, child, value + "");
VectorfindElementList(String name, String attrName, String attrValue, Document doc)
Gets a list of DOM elements with the specified name that have an attribute with the given name and value.
if (name == null)
    return null;
Vector v = new Vector();
NodeList list = doc.getElementsByTagName(name);
int size = list.getLength();
for (int i = 0; i < size; i++) {
    Node node = list.item(i);
    if (!(node instanceof Element))
...
ElementfindElementOrContainer(Document document, Element parent, String element)
find Element Or Container
NodeList nl = parent.getElementsByTagName(element);
if (nl.getLength() == 0) {
    return null;
return (Element) nl.item(0);
ElementgetElement(Document doc, QName elementQName)

Get an element from the document given its QName

First an attempt to get the element based on its namespace is made, failing which an element with the localpart ignoring any namespace is returned.

NodeList nl = doc.getElementsByTagNameNS(elementQName.getNamespaceURI(), elementQName.getLocalPart());
if (nl.getLength() == 0) {
    nl = doc.getElementsByTagNameNS("*", elementQName.getLocalPart());
    if (nl.getLength() == 0)
        nl = doc.getElementsByTagName(elementQName.getPrefix() + ":" + elementQName.getLocalPart());
    if (nl.getLength() == 0)
        return null;
return (Element) nl.item(0);
StringgetElement(Document doc, String path)
get Element
return xpath.compile(path).evaluate(doc);
ElementgetElement(Document pDocument, String psTagName, int index)
returns a XML element.
NodeList rows = pDocument.getDocumentElement().getElementsByTagName(psTagName);
return (Element) rows.item(index);