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

/**
 * Compares two DOM nodes for (deep) equality.
 * @param n1//w ww.j a v  a  2  s .co  m
 * @param n2
 * @return whether the nodes are equal
 */
public static final boolean compare(Node n1, Node n2) {
    boolean ret = true;
    ret &= n1.getLocalName().equals(n2.getLocalName());
    ret &= n1.getNamespaceURI().equals(n2.getNamespaceURI());
    String text = n1.getTextContent();
    if (text != null) {
        ret &= text.equals(n2.getTextContent());
    }
    NodeList children = n1.getChildNodes();
    ret &= (children.getLength() == n2.getChildNodes().getLength());
    for (int i = 0; ret && i < children.getLength(); i++) {
        final Node child = children.item(i);
        if (child instanceof Element) {
            ret &= compare(child, n2.getChildNodes().item(i));
        }
    }
    return ret;
}

From source file:Main.java

/**
 * @param sibling/*from w w  w.  j av  a 2  s  .  co  m*/
 * @param uri
 * @param nodeName
 * @return nodes with the constrain
 */
public static Element[] selectNodes(Node sibling, String uri, String nodeName) {
    List<Element> list = new ArrayList<Element>();
    while (sibling != null) {
        if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
                && sibling.getLocalName().equals(nodeName)) {
            list.add((Element) sibling);
        }
        sibling = sibling.getNextSibling();
    }
    return list.toArray(new Element[list.size()]);
}

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.
 *
 * @param node The node to search from./*from  ww w.j  a v  a2  s . c o  m*/
 * @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

/**
 * Dumps a debug listing of the attributes of the given element to
 * System.out./*from  www .j  ava  2  s  . c  o  m*/
 * 
 * @param elem the element to dump the attributes of
 */
public static void dumpAttrs(Element elem) {
    System.out.println("Attributes of " + elem.getNodeName() + ", NS: " + elem.getNamespaceURI());
    NamedNodeMap attrs = elem.getAttributes();
    int len = attrs.getLength();
    for (int i = 0; i < len; ++i) {
        Node attr = attrs.item(i);
        System.out.println("  Name: " + attr.getNodeName() + ", Value: " + attr.getNodeValue() + ", NS: "
                + attr.getNamespaceURI());
    }
}

From source file:Main.java

/**
 * @param root/*from  w w  w.j a  v a  2  s.  c  o m*/
 * @param uri
 * @param nodeName
 * @return nodes with the constrain
 */
public static Element selectElement(Node node, String uri, String nodeName) {
    while (node != null) {
        if (nodeName.equals(node.getLocalName()) && uri.equals(node.getNamespaceURI()))
            return (Element) node;

        Element located = findElementinChilds(node, uri, nodeName);
        if (located != null) {
            return located;
        }
    }
    return null;
}

From source file:Main.java

public static Element getChildElementByTagNameNS(Element parent, String tagName, String nsName) {
    NodeList nl = parent.getChildNodes();
    int size = nl.getLength();
    for (int i = 0; i < size; i++) {
        Node node = nl.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (tagName.equals(node.getLocalName())) {
                String ns = node.getNamespaceURI();
                if (ns != null && ns.equals(nsName)) {
                    return (Element) node;
                }/*from w  w w  .  j av a 2  s .co m*/
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * @param sibling/*from   w  w w. j  ava2  s . co  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 (nodeName.equals(sibling.getLocalName()) && uri.equals(sibling.getNamespaceURI())) {
            if (number == 0) {
                return (Element) sibling;
            }
            number--;
        }
        sibling = sibling.getNextSibling();
    }
    return null;
}

From source file:Main.java

/**
 * This method searches children of Element element for element with tagName
 * and namespaceURI nsName. It searchs one level down only.
 * /*from w  w  w .  j  av  a 2s  . c  om*/
 * @param element
 *            The root element
 * @param nsName
 *            NamespaceURI
 * @param tagName
 *            A String representing the name of the tag to be searched for.
 * @return A List of elements that meet the criterial.
 */
public static List getElementsByTagNameNS1(Element element, String nsName, String tagName) {
    List list = new ArrayList();

    if (element != null) {
        NodeList nl = element.getChildNodes();
        int length = nl.getLength();
        for (int i = 0; i < length; i++) {
            Node child = nl.item(i);
            String childName = child.getLocalName();
            String childNS = child.getNamespaceURI();
            if ((childName != null) && (childName.equals(tagName)) && (childNS != null)
                    && (childNS.equals(nsName))) {
                list.add(child);
            }
        }
    }
    return list;
}

From source file:Main.java

@Nullable
public static String getNamespaceURI(@Nullable final Node aNode) {
    if (aNode instanceof Document)
        return getNamespaceURI(((Document) aNode).getDocumentElement());
    if (aNode != null)
        return aNode.getNamespaceURI();
    return null;//w ww . ja va2s.  co m
}

From source file:Main.java

public static Vector<String> getPropertiesFromXML(Node propNode) {
    Vector<String> properties;
    properties = new Vector<String>();
    NodeList childList = propNode.getChildNodes();

    for (int i = 0; i < childList.getLength(); i++) {
        Node currentNode = childList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = currentNode.getLocalName();
            String namespace = currentNode.getNamespaceURI();
            // href is a live property which is handled differently
            properties.addElement(namespace + ":" + nodeName);
        }//from w  w w .ja va  2  s . com
    }
    return properties;
}