Java XML Node Namespace getNamespace(String prefix, Node e)

Here you can find the source of getNamespace(String prefix, Node e)

Description

get Namespace

License

Apache License

Declaration

public static String getNamespace(String prefix, Node e) 

Method Source Code


//package com.java2s;
/*/*from ww  w. j av a 2s  . co m*/
 * Copyright 1999-2006 University of Chicago
 * 
 * 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 {
    public static final String NS_URI_XMLNS = "http://www.w3.org/2000/xmlns/";

    public static String getNamespace(String prefix, Node e, Node stopNode) {
        while (e != null && (e.getNodeType() == Node.ELEMENT_NODE)) {
            Attr attr = null;
            if (prefix == null) {
                attr = ((Element) e).getAttributeNode("xmlns");
            } else {
                attr = ((Element) e).getAttributeNodeNS(NS_URI_XMLNS, prefix);
            }
            if (attr != null)
                return attr.getValue();
            if (e == stopNode)
                return null;
            e = e.getParentNode();
        }
        return null;
    }

    public static String getNamespace(String prefix, Node e) {
        return getNamespace(prefix, e, null);
    }
}

Related

  1. convertFromNamespaceForm(final Node node)
  2. copyNamespaces(Node source, Node target)
  3. getNamespace(Node node)
  4. getNamespace(Node node)
  5. getNamespaceDeclarations(Node node)
  6. getNamespaceMappings(Node node)
  7. getNamespaces(Node node, Map list)
  8. getNamespaceURI(@Nullable final Node aNode)