Java XML Node Text Value getNamespaceURIFromPrefix(Node context, String prefix)

Here you can find the source of getNamespaceURIFromPrefix(Node context, String prefix)

Description

Given a prefix and a node, return the namespace URI that the prefix has been associated with.

License

Apache License

Parameter

Parameter Description
context the starting node (looks up recursively from here)
prefix the prefix to find an xmlns:prefix=uri for

Return

the namespace URI or null if not found

Declaration

public static String getNamespaceURIFromPrefix(Node context,
        String prefix) 

Method Source Code

//package com.java2s;
/*/*from   w  ww .  j av a  2 s  . co  m*/
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.w3c.dom.Attr;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /**
     * The namespaceURI represented by the prefix <code>xmlns</code>.
     */
    private static String NS_URI_XMLNS = "http://www.w3.org/2000/xmlns/";

    /**
     * Given a prefix and a node, return the namespace URI that the prefix
     * has been associated with. This method is useful in resolving the
     * namespace URI of attribute values which are being interpreted as
     * QNames. If prefix is null, this method will return the default
     * namespace.
     *
     * @param context the starting node (looks up recursively from here)
     * @param prefix the prefix to find an xmlns:prefix=uri for
     *
     * @return the namespace URI or null if not found
     */
    public static String getNamespaceURIFromPrefix(Node context,
            String prefix) {
        short nodeType = context.getNodeType();
        Node tempNode = null;

        switch (nodeType) {
        case Node.ATTRIBUTE_NODE: {
            tempNode = ((Attr) context).getOwnerElement();
            break;
        }
        case Node.ELEMENT_NODE: {
            tempNode = context;
            break;
        }
        default: {
            tempNode = context.getParentNode();
            break;
        }
        }

        while (tempNode != null
                && tempNode.getNodeType() == Node.ELEMENT_NODE) {
            Element tempEl = (Element) tempNode;
            String namespaceURI = (prefix == null) ? getAttribute(tempEl,
                    "xmlns") : getAttributeNS(tempEl, NS_URI_XMLNS, prefix);

            if (namespaceURI != null) {
                return namespaceURI;
            } else {
                tempNode = tempEl.getParentNode();
            }
        }

        return null;
    }

    /**
     * Returns the value of an attribute of an element. Returns null
     * if the attribute is not found (whereas Element.getAttribute
     * returns "" if an attrib is not found).
     *
     * @param el       Element whose attrib is looked for
     * @param attrName name of attribute to look for
     * @return the attribute value
     */
    static public String getAttribute(Element el, String attrName) {
        String sRet = null;
        Attr attr = el.getAttributeNode(attrName);

        if (attr != null) {
            sRet = attr.getValue();
        }
        return sRet;
    }

    /**
     * Returns the value of an attribute of an element. Returns null
     * if the attribute is not found (whereas Element.getAttributeNS
     * returns "" if an attrib is not found).
     *
     * @param el       Element whose attrib is looked for
     * @param namespaceURI namespace URI of attribute to look for
     * @param localPart local part of attribute to look for
     * @return the attribute value
     */
    static public String getAttributeNS(Element el, String namespaceURI,
            String localPart) {
        String sRet = null;
        Attr attr = el.getAttributeNodeNS(namespaceURI, localPart);

        if (attr != null) {
            sRet = attr.getValue();
        }

        return sRet;
    }
}

Related

  1. getFirstLevelTextContent(Node node)
  2. getFirstSubElementsInnerText(Node element, String subElementName)
  3. getInnerXmlText(Node xmlNode)
  4. getNamedItemText(NamedNodeMap map, String name)
  5. getNamespaceURIFromPrefix(Node context, String prefix)
  6. getNodeText(final Node node)
  7. getNodeText(Node node)
  8. getNodeText(Node node)
  9. getNodeText(Node node)