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

/**
 * Returns a Map of all attributes of the given element with the given namespace.
 * Why can we not create our own NamedNodeMaps? This would save trouble.
 * @param element the elment from which to retrieve the attributes.
 * @param namespaceURI the namespace of the attributes to retrieve.
 * @return a Map containing the attributes names and their values.
 *//*  ww w . j  a v a 2 s .  c  om*/
public static Map getAttributesWithNS(Element element, String namespaceURI) {
    Map result = new HashMap();

    NamedNodeMap attributes = element.getAttributes();

    if (attributes == null)
        return result;

    for (int i = 0; i != attributes.getLength(); i++) {
        Node attribute = attributes.item(i);

        if (namespaceURI == null && attribute.getNamespaceURI() == null) {
            result.put(attribute.getNodeName(), attribute.getNodeValue());
        } else if (attribute.getNamespaceURI() != null && attribute.getNamespaceURI().equals(namespaceURI)) {
            result.put(attribute.getNodeName(), attribute.getNodeValue());
        }
    }

    return result;
}

From source file:Main.java

/**
 * Recursively fetches all nodes of specified ns. For multi ns documents
 *
 * @param node the starting node./*from  w ww.  ja  va2s. c o m*/
 * @param namespace desired ns
 */
private static List<Node> getAllNodesByNamespaceRecursive(Node node, String namespace) {
    List nsNodeList = new ArrayList();
    if (node.getNamespaceURI() != null && node.getNamespaceURI().equals(namespace)) {
        nsNodeList.add(node);
    }
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        nsNodeList.addAll(getAllNodesByNamespaceRecursive(list.item(i), namespace));
    }
    return nsNodeList;
}

From source file:Main.java

private static int findNodeIndex(Node node) {
    String nm = node.getLocalName();
    String ns = node.getNamespaceURI();
    short nt = node.getNodeType();

    Node parentNode = node.getParentNode();
    if (parentNode.getNodeType() != Node.ELEMENT_NODE)
        return 1;

    Node child = parentNode.getFirstChild();

    int ix = 0;/*  w  ww .  jav  a  2  s  .c  o m*/
    while (child != null) {
        if (child == node)
            return ix + 1;

        if (child.getNodeType() == nt && nm.equals(child.getLocalName())
                && ((ns == null && child.getNamespaceURI() == null)
                        || (ns != null && ns.equals(child.getNamespaceURI()))))
            ix++;

        child = child.getNextSibling();
    }

    throw new RuntimeException("Child node not found in parent!?");
}

From source file:Main.java

/**
 * Dumps a debug listing of the child nodes of the given node to
 * System.out.//from w w  w  .  jav  a2 s .  c om
 * 
 * @param node the node to dump the children of
 */
public static void dumpChildren(Node node) {
    System.out.println("Children of " + node.getNodeName() + ", NS: " + node.getNamespaceURI() + ", Type: "
            + node.getClass());
    Node child = node.getFirstChild();
    while (child != null) {
        short nodeType = child.getNodeType();
        String nodeName = child.getNodeName();
        String nodeValue = child.getNodeValue();
        String nsURI = child.getNamespaceURI();
        System.out.println("  Type: " + nodeType + ", Name: " + nodeName + ", Value: " + nodeValue + ", NS: "
                + nsURI + ", Type: " + node.getClass());
        child = child.getNextSibling();
    }
}

From source file:Main.java

/**
 * Gets the qualified name of a DOM node.
 * //w ww.ja v  a  2 s  .  c o m
 * @param node
 *            A DOM node.
 * @return A QName representing a qualified name.
 */
public static QName getQName(Node node) {
    String localName = (null == node.getLocalName()) ? "" : node.getLocalName();
    return new QName(node.getNamespaceURI(), localName);
}

From source file:Main.java

public static boolean hasNamespaceURI(@Nullable final Node aNode, @Nullable final String sNamespaceURI) {
    final String sNSURI = aNode == null ? null : aNode.getNamespaceURI();
    return sNSURI != null && sNSURI.equals(sNamespaceURI);
}

From source file:Main.java

static List<Element> findChildren(Element element, String ns, String tag) {
    final List<Element> list = new ArrayList<Element>();
    for (Node node : listOf(element.getChildNodes())) {
        if (tag.equals(node.getLocalName()) && ((ns == null) || node.getNamespaceURI().equals(ns))) {
            list.add((Element) node);
        }/*from  w  w w  . java 2 s.c  o m*/
    }
    return list;
}

From source file:Main.java

/**
 * @param sibling/*from  w  w w. j a v a2  s  .c o  m*/
 * @param uri
 * @param nodeName
 * @param number
 * @return nodes with the constrain
 */
public static Element selectNode(Node sibling, String uri, String nodeName, int number) {
    while (sibling != null) {
        if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
                && sibling.getLocalName().equals(nodeName)) {
            if (number == 0) {
                return (Element) sibling;
            }
            number--;
        }
        sibling = sibling.getNextSibling();
    }
    return null;
}

From source file:Main.java

/**
 * Returns a List of all descendant Element nodes having the specified
 * [namespace name] property. The elements are listed in document order.
 * /*from  w w w. ja v a 2  s  .c  o  m*/
 * @param node
 *            The node to search from.
 * @param namespaceURI
 *            An absolute URI denoting a namespace name.
 * @return A List containing elements in the specified namespace; the list
 *         is empty if there are no elements in the namespace.
 */
public static List<Element> getElementsByNamespaceURI(Node node, String namespaceURI) {
    List<Element> list = new ArrayList<Element>();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() != Node.ELEMENT_NODE)
            continue;
        if (child.getNamespaceURI().equals(namespaceURI))
            list.add((Element) child);
    }
    return list;
}

From source file:Main.java

private static List<Element> find(Node root, String nsUri, String localName, boolean recursive) {
    List<Element> result = new ArrayList<Element>();
    NodeList nl = root.getChildNodes();
    if (nl != null) {
        int len = nl.getLength();
        for (int i = 0; i < len; i++) {
            Node child = nl.item(i);
            if (child instanceof Element) {
                String childUri = child.getNamespaceURI();
                String childLocalName = child.getLocalName();
                if (("*".equals(nsUri) || nsUri == null && childUri == null || nsUri.equals(childUri))
                        && localName.equals(childLocalName)) {
                    result.add((Element) child);
                } else if (recursive) {
                    result.addAll(find(child, nsUri, localName, recursive));
                }//from w ww.  ja va  2s . c om
            }
        }
    }
    return result;
}