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

StringgetElementName(Object obj)
Returns the element name of the spedified object.
XmlType xmlType = obj.getClass().getAnnotation(XmlType.class);
if (xmlType != null && xmlType.name() != null) {
    return xmlType.name();
return null;
StringgetElementNameSansNamespace(Element element)
get Element Name Sans Namespace
String name = element.getNodeName();
int colonIndex = name.indexOf(':');
if (colonIndex != -1) {
    return name.substring(colonIndex + 1);
return name;
voidgetElementNamespaces(Element element, Set namespaces)
Gets the namespaces declared on the element and adds them to the specified set.
NamedNodeMap atts = element.getAttributes();
for (int i = 0; i < atts.getLength(); i++) {
    Attr att = (Attr) (atts.item(i));
    String prefix = att.getPrefix();
    if (prefix != null && prefix.equals("xmlns")) {
        String nsURI = att.getValue();
        namespaces.add(nsURI);
    } else {
...
StringgetElementNamespaceURI(Element element)
get Element Namespace URI
String namespace = null;
if (element == null) {
    throw new IllegalArgumentException("The element cannot be null");
} else {
    try {
        namespace = element.getNamespaceURI();
    } catch (NoSuchMethodError e) {
        String tagName = element.getTagName();
...
ElementgetElementNS(Element el, String nsuri, String name)
get Element NS
Node node = el.getFirstChild();
while (node != null) {
    if (matchNS(node, nsuri, name)) {
        return (Element) node;
    node = node.getNextSibling();
return null;
...
ElementgetElementNS(Element elem, String namespaceUri, String localName)
Get the first child with the specified name in the specified namespace.
NodeList nl = elem.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        if ((node.getNamespaceURI() != null && node.getNamespaceURI().equals(namespaceUri))
                && node.getLocalName().equals(localName)) {
            return (Element) node;
return null;
ElementgetElementNS(Element root, Set nsUris, String wantedLocalName)
Searches for the first sub-element of the given XML element, which has the given local name and whose namespace belongs to the given set.
if (root == null) {
    return null;
Node node = root.getFirstChild();
while (node != null) {
    if ((node instanceof Element) && nsUris.contains(node.getNamespaceURI())
            && node.getLocalName().equals(wantedLocalName)) {
        return (Element) node;
...
StringgetElementPath(Element elem)
Get the path to a given element.
if (elem == null)
    return null;
StringBuffer sb = new StringBuffer();
Node parent = null;
for (Node node = elem; node != null; node = parent) {
    parent = node.getParentNode();
    if ((node instanceof Element) && (parent instanceof Element)) {
        if (sb.length() > 0)
...
StringgetElementPath(Element element)
Returns absolute xpath for specified element, ignores namespaces
Node elm = element;
String result = elm.getNodeName() + "[" + getElementIndex(elm) + "]";
while (elm.getParentNode() != null && elm.getParentNode().getNodeType() != Node.DOCUMENT_NODE) {
    elm = elm.getParentNode();
    result = elm.getNodeName() + "[" + getElementIndex(elm) + "]/" + result;
return "/" + result;
StringgetElementPath(Element element)
Returns absolute xpath for specified element, ignores namespaces
Node elm = element;
String result = elm.getNodeName() + "[" + getElementIndex(elm) + "]";
while (elm.getParentNode() != null && elm.getParentNode().getNodeType() != Node.DOCUMENT_NODE) {
    elm = elm.getParentNode();
    result = elm.getNodeName() + "[" + getElementIndex(elm) + "]/" + result;
return "/" + result;