Java XML Attribute By QName getAttributeValueAsQName(final Element el, final String attrName)

Here you can find the source of getAttributeValueAsQName(final Element el, final String attrName)

Description

get Attribute Value As Q Name

License

Apache License

Declaration

public static QName getAttributeValueAsQName(final Element el, final String attrName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.xml.namespace.QName;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    public static QName getAttributeValueAsQName(final Element el, final String attrName) {
        return getAttributeValueAsQName(el, new QName(attrName));
    }/*w ww.  ja  v a  2 s  .com*/

    public static QName getAttributeValueAsQName(final Element el, final QName attrName) {
        QName qname = null;
        final String qualifiedName = getAttributeValue(el, attrName);
        if (qualifiedName != null) {
            qname = resolveQName(el, qualifiedName);
        }
        return qname;
    }

    public static String getAttributeValue(final Element el, final String attrName) {
        return getAttributeValue(el, new QName(attrName));
    }

    public static String getAttributeValue(final Element el, final String attrName, final String defaultVal) {
        final String retval = getAttributeValue(el, new QName(attrName));
        return retval == null ? defaultVal : retval;
    }

    public static String getAttributeValue(final Element el, final QName attrName) {
        String attr = null;
        if ("".equals(attrName.getNamespaceURI())) {
            attr = el.getAttribute(attrName.getLocalPart());
        } else {
            attr = el.getAttributeNS(attrName.getNamespaceURI(), attrName.getLocalPart());
        }

        if ("".equals(attr)) {
            attr = null;
        }
        return attr;
    }

    public static QName resolveQName(final Element el, final String qualifiedName) {
        QName qname;
        String prefix = "";
        String namespaceURI = "";
        String localPart = qualifiedName;

        final int colIndex = qualifiedName.indexOf(":");
        if (colIndex > 0) {
            prefix = qualifiedName.substring(0, colIndex);
            localPart = qualifiedName.substring(colIndex + 1);

            if ("xmlns".equals(prefix)) {
                namespaceURI = "URI:XML_PREDEFINED_NAMESPACE";
            } else {
                Element nsElement = el;
                while (namespaceURI.equals("") && nsElement != null) {
                    namespaceURI = nsElement.getAttribute("xmlns:" + prefix);
                    if (namespaceURI.equals("")) {
                        nsElement = getParentElement(nsElement);
                    }
                }
            }
            if (namespaceURI.equals("")) {
                throw new IllegalArgumentException("Cannot find namespace uri for: " + qualifiedName);
            }
        }

        qname = new QName(namespaceURI, localPart, prefix);
        return qname;
    }

    public static Element getParentElement(final Node node) {
        final Node parent = node.getParentNode();
        return parent instanceof Element ? (Element) parent : null;
    }
}

Related

  1. getAttribute(Element el, QName attributeName)
  2. getAttribute(Element xml, QName qname)
  3. getAttributeAsQName(XMLStreamReader reader, String name)
  4. getAttributeValue(final XMLStreamReader streamReader, final QName name)