Example usage for org.w3c.dom Element getElementsByTagNameNS

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

Introduction

In this page you can find the example usage for org.w3c.dom Element getElementsByTagNameNS.

Prototype

public NodeList getElementsByTagNameNS(String namespaceURI, String localName) throws DOMException;

Source Link

Document

Returns a NodeList of all the descendant Elements with a given local name and namespace URI in document order.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*from  w w w  .  j ava2s . c om*/
    factory.setValidating(true);

    DocumentBuilder loader = factory.newDocumentBuilder();
    Document document = loader.parse("sample.xml");

    String docNS = "http://www.my-company.com";

    Element order = document.getDocumentElement();
    Attr date = order.getAttributeNodeNS(docNS, "date");
    NodeList children = order.getElementsByTagNameNS(docNS, "item");

}

From source file:Main.java

public static Element findElement(Element parent, String uri, String localName) {
    NodeList nl = parent.getElementsByTagNameNS(uri, localName);
    if (nl.getLength() > 0) {
        return (Element) nl.item(0);
    } else {/*www  .j a v  a2  s .  co m*/
        return null;
    }
}

From source file:Main.java

public static int getSizeNS(Element el, String nameSpaceURI, String tagName) {
    NodeList list = el.getElementsByTagNameNS(nameSpaceURI, tagName);
    return list.getLength();
}

From source file:Main.java

/**
 * Gets the first child Element with a given name
 *
 * @param n the node get the children from
 * @param namespace the namespace of the child element
 * @param elementName the name of the child elements
 * @return the first child Element with a given name
 *//*from   ww w .ja v  a  2s. c  om*/
public static Element getElementByQualifiedName(Element n, String namespace, String elementName) {
    NodeList subNodes = n.getElementsByTagNameNS(namespace, elementName);
    int sz = subNodes.getLength();
    if (sz > 0) {
        return (Element) subNodes.item(0);
    }
    return null;
}

From source file:Main.java

/**
 * Gets the first child Element with a given name
 * /*from  w  w w. j a  va 2  s . com*/
 * @param n
 *            the node get the children from
 * @param namespace
 *            the namespace of the child element
 * @param elementName
 *            the name of the child elements
 * @return the first child Element with a given name
 */
public static Element getElementByQualifiedName(Element n, String namespace, String elementName) {
    NodeList subNodes = n.getElementsByTagNameNS(namespace, elementName);
    int sz = subNodes.getLength();
    if (sz > 0)
        return (Element) subNodes.item(0);
    return null;
}

From source file:DOMHelper.java

/**
 * Gets the first element with the specified qualified name that is descendant
 * of {@code e}.//  w  ww.j  a v  a 2s.  c  o m
 * @return the element or {@code null} if there is no such element
 */
public static Element getFirstDescendant(Element e, String nameSpace, String localName) {
    return (Element) e.getElementsByTagNameNS(nameSpace, localName).item(0);
}

From source file:Main.java

public static Element getElementNS(Element el, String nameSpaceURI, String tagName, int index) {
    NodeList list = el.getElementsByTagNameNS(nameSpaceURI, tagName);
    return (Element) list.item(index);
}

From source file:Main.java

/**
 *
 * @param element Element/*from  w w  w.  j a v a  2  s .  co m*/
 * @param namespace String
 * @param childName String
 * @return String
 */
public static String getChildStringNS(Element element, String namespace, String childName) {
    NodeList nodes = element.getElementsByTagNameNS(namespace, childName);
    if (nodes.getLength() > 0) {
        Node fnd = nodes.item(0).getFirstChild();
        if (fnd != null)
            return fnd.getNodeValue();
    }
    return "";
}

From source file:Main.java

private static Node getSignatureNode(final Element rootElement) throws SignatureException {
    final NodeList nl = rootElement.getElementsByTagNameNS(XMLSignature.XMLNS, NODE_SIGNATURE);
    if (nl.getLength() == 0) {
        throw new SignatureException("Cannot find Signature element");
    }// w w  w .  j  a va 2 s .co  m
    return nl.item(0);
}

From source file:Main.java

/**
 * Extracts the content of the element named <var>name</var> with the namespace
 * <var>elementNS</var> that is a descentant of the element
 * <var>message</var>. This method returns null if the named elemens is not
 * a child of <var>message</var> or if <var>message</var> contains more than
 * one element named <var>name</var>
 * //from   ww  w  .j  a  v  a  2s .  c  o  m
 * @param message
 *            Source Document that contains the element named
 *            <var>name</var>
 * @param elementNS
 *            Namespace of the element named <var>name</var>
 * @param name
 *            Name of the element to be extracted
 * @return Element named <var>name</var>, <br>
 *         or null if <var>message</var> contains zero or more than one
 *         element named <var>name</var>
 */
public static String extractElementText(Element message, String elementNS, String name, boolean strict) {
    NodeList list = message.getElementsByTagNameNS(elementNS, name);
    if (list == null || list.getLength() == 0 || (strict && list.getLength() != 1))
        return null;
    Element element = (Element) list.item(0);
    return element.getTextContent();
}