Java XML Node Namespace getNodeName(Node node, NamespaceContext namespaceContext)

Here you can find the source of getNodeName(Node node, NamespaceContext namespaceContext)

Description

Returns the node name using the prefixes defined in the given namespace context rather than the prefixes used in the parsed XML.

License

Open Source License

Parameter

Parameter Description
node The node to return the name of.
namespaceContext The namespace context to use for prefixing qualified names.

Return

The name of the node.

Declaration

private static String getNodeName(Node node, NamespaceContext namespaceContext) 

Method Source Code

//package com.java2s;
/*//  www. j  a va2  s . c om
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Lachlan Dowding
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import org.w3c.dom.Node;

import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;

public class Main {
    /**
     * Returns the node name using the prefixes defined in the given namespace context rather than
     * the prefixes used in the parsed XML.
     *
     * @param node              The node to return the name of.
     * @param namespaceContext  The namespace context to use for prefixing qualified names.
     * @return                  The name of the node.
     */
    private static String getNodeName(Node node, NamespaceContext namespaceContext) {
        if (node == null)
            return null;

        String name = node.getNodeName();
        if (name.startsWith("xmlns:")) {
            String uri = node.getNodeValue();
            if (uri != null && namespaceContext != null) {
                String prefix = namespaceContext.getPrefix(uri);
                if (prefix != null) {
                    if (!prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
                        // correct the prefix on the namespace declaration, if using a different one when parsing
                        name = "xmlns:" + prefix;
                    }
                }
            }
        } else {
            String uri = node.getNamespaceURI();
            if (uri != null && namespaceContext != null) {
                String prefix = namespaceContext.getPrefix(uri);
                if (prefix != null) {
                    name = node.getLocalName();
                    if (!prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
                        name = prefix + ":" + name;
                    }
                }
            }
        }
        return name;
    }
}

Related

  1. getNamespaceURI(Node node)
  2. getNamespaceUri(Node node)
  3. getNamespaceURI(Node node)
  4. getNameSpaceUri(Node node, String content, String namespaceURI)
  5. getNamespaceURI(Node node, String searchPrefix)
  6. getNodeString(Node node, NamespaceContext context)
  7. hasDITANamespace(Node node)
  8. hasNamespace(Node node, String prefix, String namespace)
  9. hasNamespaceURI(@Nullable final Node aNode, @Nullable final String sNamespaceURI)