Example usage for org.w3c.dom Node getNamespaceURI

List of usage examples for org.w3c.dom Node getNamespaceURI

Introduction

In this page you can find the example usage for org.w3c.dom Node getNamespaceURI.

Prototype

public String getNamespaceURI();

Source Link

Document

The namespace URI of this node, or null if it is unspecified (see ).

Usage

From source file:Main.java

public static QName createQName(Node node) {
    return new QName(node.getNamespaceURI(), node.getLocalName());
}

From source file:Main.java

public static QName nodeToQName(Node node) {
    return new QName(node.getNamespaceURI(), node.getLocalName());
}

From source file:Main.java

/**
 * Get the namespace URI of a given node.
 *
 * @param node The node to get the namespace URI of.
 * @return The namespace URI, or the null namespace URI if the node has none.
 *///  w ww . j a v  a 2 s  . co  m
public static String getNamespaceUri(Node node) {
    String namespace = node.getNamespaceURI();
    return (namespace != null) ? namespace : XMLConstants.NULL_NS_URI;
}

From source file:Main.java

/**
 * Determines if a node has a particular qualified name.
 * @param node the node/*from   w ww  .j  a v a  2 s  .c o m*/
 * @param qname the qualified name
 * @return true if the node has the given qualified name, false if not
 */
public static boolean hasQName(Node node, QName qname) {
    return qname.getNamespaceURI().equals(node.getNamespaceURI())
            && qname.getLocalPart().equals(node.getLocalName());
}

From source file:Main.java

static List<Node> children(Node parent, String childNS, String... childLocalName) {
    List<String> childNames = Arrays.asList(childLocalName);
    ArrayList<Node> result = new ArrayList<Node>();

    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (childNS.equals(child.getNamespaceURI()) && childNames.contains(child.getLocalName())) {

            result.add(child);/*  ww w.ja  va 2  s.  c  o  m*/
        }
    }
    return result;
}

From source file:Main.java

public static QName getQName(Node n) {
    if (n.getLocalName() != null) {
        return new QName(n.getNamespaceURI(), n.getLocalName());
    } else {/*w  w w .  j  ava2s. c o  m*/
        return new QName(n.getNamespaceURI(), n.getNodeName());
    }
}

From source file:Main.java

private static String getNodeNameWithNamespace(Node node) {
    String name = node.getNodeName();
    String ns = node.getNamespaceURI();
    String prefix = (ns != null) ? node.lookupPrefix(ns) : null;
    if ((prefix != null) && !name.startsWith(prefix + ":")) {
        name = prefix + ":" + name;
    }//w  w  w  . j  av a 2 s  .c  o  m
    return name;
}

From source file:Main.java

/**
 * Find a Node with a given QName//from   ww  w .  j  a  v a 2 s.  c  om
 *
 * @param node parent node
 * @param name QName of the child we need to find
 * @return child node
 */
public static Node findNode(Node node, QName name) {
    if (name.getNamespaceURI().equals(node.getNamespaceURI())
            && name.getLocalPart().equals(node.getLocalName()))
        return node;
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node ret = findNode(children.item(i), name);
        if (ret != null)
            return ret;
    }
    return null;
}

From source file:Main.java

public static void removeNamespaceDeclarations(Node node, String... namespaces) {
    NamedNodeMap namedNodeMap = ((Element) node).getAttributes();
    for (int nameIndex = 0; nameIndex < namedNodeMap.getLength(); nameIndex++) {
        Node namedNode = namedNodeMap.item(nameIndex);
        String uri = namedNode.getNamespaceURI();
        String localName = namedNode.getLocalName();
        if (uri != null && uri.equals("http://www.w3.org/2000/xmlns/")) {
            for (String removeableNamespace : namespaces) {
                if (namedNode.getNodeValue().equals(removeableNamespace)) {
                    ((Element) node).removeAttributeNS("http://www.w3.org/2000/xmlns/", localName);
                    nameIndex--;//w  w  w. j a va2s . c  o m
                }
            }
        }
    }
}

From source file:Main.java

/**
 * Finds a Node with a given QNameb.//  w w  w  .  j  a va 2  s  . co  m
 *
 * @param node parent node
 * @param name QName of the child we need to find
 * @return Returns child node.
 */
public static Node findNode(Node node, QName name) {
    if (name.getNamespaceURI().equals(node.getNamespaceURI())
            && name.getLocalPart().equals(node.getLocalName())) {
        return node;
    }
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node ret = findNode(children.item(i), name);
        if (ret != null) {
            return ret;
        }
    }
    return null;
}