Java Utililty Methods XML Element Namespace

List of utility methods to do XML Element Namespace

Description

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

Method

NodeListgetElementListNS(String namespace, Element element, String name)
Return a list of specified namespace:Elements
return element.getElementsByTagNameNS(namespace, name);
ElementgetElementNS(String namespace, Element element, String name)
Return the first named Element found - namespace aware
NodeList nodeList = getElementListNS(namespace, element, name);
return (nodeList.getLength() == 0) ? null : (Element) nodeList.item(0);
QNamegetNamespace(Element element)
get Namespace
String uri = element.getNamespaceURI();
String prefix = element.getPrefix() == null ? "" : element.getPrefix();
if (uri == null) {
    return new QName("");
} else {
    return new QName(uri, element.getLocalName(), prefix);
NodegetNamespaceDeclarationOrNull(String localName, Element element)
get Namespace Declaration Or Null
NamedNodeMap map = element.getAttributes();
for (int i = 0; i < map.getLength(); i++) {
    Node node = map.item(i);
    String nsUri = node.getNamespaceURI();
    if (nsUri == null) {
        continue;
    if ("http://www.w3.org/2000/xmlns/".equals(nsUri) && localName.equals(node.getLocalName())) {
...
StringgetNamespaceForPrefix(String prefix, Element element)
get Namespace For Prefix
NamedNodeMap attributes = element.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
    Node node = attributes.item(i);
    if (node instanceof Attr) {
        Attr attr = (Attr) node;
        if (XML_NS_NS.equals(attr.getNamespaceURI())) {
            if (attr.getLocalName().equals(prefix)) {
                return attr.getValue();
...
StringgetNamespaceURI(Element element, String prefix)
get Namespace URI
if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
    prefix = null;
return element.lookupNamespaceURI(prefix);
StringgetNamespaceUriDeclaration(Element ele)
get Namespace Uri Declaration
NamedNodeMap attribs = ele.getAttributes();
for (int i = 0; i < attribs.getLength(); i++) {
    Attr attr = (Attr) attribs.item(i);
    if ("xmlns".equals(attr.getLocalName()) || XMLConstants.XML_NS_URI.equals(attr.getNamespaceURI())) {
        return attr.getTextContent();
return "";
...
NodegetRequiredNamespaceDeclaration(String localName, Element element)
get Required Namespace Declaration
Node node = getNamespaceDeclarationOrNull(localName, element);
if (node == null) {
    throw new IllegalStateException(
            String.format("Namespace declaration for prefix '%s' not found", localName));
return node;
booleanisAppropriateElement(final Node iNode, final String iNodeName, final String iNamespace)
This method determins if a node in the DOM Tree (iNode) is the node we are looking for.
boolean result = false;
if (iNode.getNodeType() == Node.ATTRIBUTE_NODE) {
    if (iNode.getNamespaceURI() == null) {
        String parentsNamespace = ((Attr) iNode).getOwnerElement().getNamespaceURI();
        if ((iNode.getLocalName().equals(iNodeName)) && (parentsNamespace.equals(iNamespace))) {
            result = true;
    } else {
...
booleanisSameElement(final String namespace, final String localName, final XMLStreamReader reader)
Checks whether XML stream reader namespace and local name match the one specified in the call.
return namespace.equals(reader.getNamespaceURI()) && localName.equals(reader.getLocalName());