Example usage for org.w3c.dom Element getNamespaceURI

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

Introduction

In this page you can find the example usage for org.w3c.dom Element 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 List<Element> getElements(Node parent, String namespaceURI, String localName) {
    List<Element> ret = new ArrayList<Element>();
    for (Node node : getNonEmptyChildNodes(parent)) {
        if (!(node instanceof Element))
            continue;
        Element elm = (Element) node;
        if ((namespaceURI == null && elm.getNamespaceURI() == null
                || namespaceURI != null && namespaceURI.equals(elm.getNamespaceURI()))
                && elm.getLocalName().equals(localName)) {
            ret.add((Element) node);
        }//from  w  ww .ja v  a  2s.co  m
    }
    return ret;
}

From source file:Main.java

public static List<Element> elements(Element root, String namepaceUri, String localName) {
    List<Element> L = new ArrayList<Element>();
    if (root == null)
        return L;
    for (Node n1 = root.getFirstChild(); n1 != null; n1 = n1.getNextSibling()) {
        if (n1.getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element e2 = Element.class.cast(n1);
        if (namepaceUri.equals(e2.getNamespaceURI()) && e2.getLocalName().equals(localName)) {
            L.add(e2);//  w  ww  . ja  v  a  2s.com
        }
    }
    return L;
}

From source file:Main.java

public static ArrayList<Element> getElementsByTagNameNS(Node element, String namespace, String name) {
    ArrayList<Element> childElements = new ArrayList<Element>();

    NodeList elements = element.getChildNodes();
    if (elements != null) {
        for (int i = 0; i < elements.getLength(); i++) {
            if (elements.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element currentElement = (Element) elements.item(i);
                if ((currentElement.getNamespaceURI() == null
                        || currentElement.getNamespaceURI().equals(namespace))
                        && currentElement.getNodeName().equals(name)) {

                    childElements.add(currentElement);
                }//  w  ww  . j  ava2  s . c o  m
            }
        }
    }

    return childElements;
}

From source file:Main.java

private static int getChildrenCount(Element parentNode, String namespaceURI, String localName) {
    int counter = 0;

    List<Element> children = getChildElements(parentNode);
    for (Element c : children) {
        if (namespaceURI.equals(c.getNamespaceURI()) && localName.equals(c.getLocalName())) {
            counter++;//www. j a  v  a2 s  . c o  m
        }
    }

    return counter;
}

From source file:Main.java

/**
 * Dumps a debug listing of the attributes of the given element to
 * System.out.//from   w w  w .jav  a 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

public static Element getChildElement(Element parent, String childName, String childNamespace) {
    NodeList ns = parent.getChildNodes();
    for (int i = 0; i < ns.getLength(); i++) {
        Node n = ns.item(i);//from   w w w  .  j ava  2  s.  c  o  m
        if (n instanceof Element) {
            Element child = (Element) n;
            if (childName.equals(child.getLocalName()) && childNamespace.equals(child.getNamespaceURI())) {
                return child;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * recursively transform the prefix and the namespace of a node where getNamespaceURI is NULL
 * @param node the node to transform/*from   w w w.  j  av  a  2 s  . c o m*/
 * @param prefix the new prefix
 * @param namespaceuri the new namespace uri
 * @return the new node with NS and prefix changed
 */
static public Node setPrefixNamespace(Node node, String prefix, String namespaceuri) {
    Node dest = null;
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element e = (Element) node;
        if (e.getNamespaceURI() == null) {

            Element e2 = node.getOwnerDocument().createElementNS(namespaceuri,
                    (prefix != null ? prefix + ':' + e.getLocalName() : e.getLocalName()));
            NamedNodeMap nodes = e.getAttributes();
            for (int i = 0; i < nodes.getLength(); ++i) {
                Attr att = (Attr) (node.getOwnerDocument().importNode(nodes.item(i), true));
                e2.setAttributeNode(att);
            }
            dest = e2;
        } else {
            dest = node.getOwnerDocument().importNode(node, false);
        }
    } else {
        dest = node.getOwnerDocument().importNode(node, false);
    }

    for (Node c = node.getFirstChild(); c != null; c = c.getNextSibling()) {
        dest.appendChild(setPrefixNamespace(c, prefix, namespaceuri));
    }
    return dest;
}

From source file:Main.java

public static List<Element> getElementsByTagNameNS(Element parent, String uri, String name, boolean localOnly) {
    List<Element> ret = new ArrayList<Element>();
    if (!localOnly) {
        NodeList elementList = parent.getElementsByTagNameNS(uri, name);
        for (int i = 0; i < elementList.getLength(); i++) {
            ret.add((Element) elementList.item(i));
        }//from w ww . j a v  a 2 s.c om
    } else {
        NodeList childList = parent.getChildNodes();
        for (int i = 0; i < childList.getLength(); i++) {
            if (childList.item(i).getNodeType() != Node.ELEMENT_NODE)
                continue;
            Element child = (Element) childList.item(i);
            if ((uri.equals("*") || child.getNamespaceURI().equals(uri))
                    && ((child.getLocalName() == null && uri.equals("*") && child.getTagName().equals(name))
                            || child.getLocalName().equals(name)))
                ret.add(child);
        }
    }
    return ret;
}

From source file:Main.java

static Element findChild(Element element, String ns, String tag) {
    final NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        if (childNodes.item(i) instanceof Element) {
            Element child = (Element) childNodes.item(i);
            if (child.getLocalName().equals(tag) && (ns == null || child.getNamespaceURI().equals(ns))) {
                return child;
            }//from  ww w . j  ava  2s . c o  m
        }
    }
    return null;
}

From source file:Main.java

public static List<Element> elementsQName(Element element, Set<QName> allowedTagNames) {
    if (element == null || !element.hasChildNodes()) {
        return Collections.emptyList();
    }// w ww.  j  a  va  2  s.com

    List<Element> elements = new ArrayList<Element>();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = (Element) child;
            QName childQName = new QName(childElement.getNamespaceURI(), childElement.getLocalName());
            if (allowedTagNames.contains(childQName)) {
                elements.add(childElement);
            }
        }
    }
    return elements;
}