Java XML QName resolveQName(final Element el, final String qualifiedName)

Here you can find the source of resolveQName(final Element el, final String qualifiedName)

Description

resolve Q Name

License

Apache License

Declaration

public static QName resolveQName(final Element el, final String qualifiedName) 

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 resolveQName(final Element el, final String qualifiedName) {
        QName qname;/* w  w  w  . j  a va  2 s.c o m*/
        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. parseXml(final Node node, final Map properties)
  2. printPath(List path)
  3. read(Node node, String expression, QName returnType)
  4. readTextElement(XMLEventReader reader, QName elemName)
  5. resolveNamespace(String qname, Node xml)
  6. resolveQName(String qNameWithPrefix, Element element)
  7. search(List list, Element baseElement, QName nodeName, boolean recursive)
  8. serializeJavaToXml(QName qname, Serializable value)
  9. serializeQName(QName qName)