Example usage for org.w3c.dom Element getElementsByTagName

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

Introduction

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

Prototype

public NodeList getElementsByTagName(String name);

Source Link

Document

Returns a NodeList of all descendant Elements with a given tag name, in document order.

Usage

From source file:Main.java

public static Element getElement(Element parentElement, String nodeName) {
    NodeList list = parentElement.getElementsByTagName(nodeName);
    if (list == null || list.getLength() == 0) {
        return null;
    }//from   w ww .  j a v a  2s .  c o  m
    return (Element) list.item(0);
}

From source file:Main.java

/**
 * @return the child element, throws exception if the parent does not
 *          have exactly one sub element with name
 *//*w  w  w  . j a v a2  s. c om*/
public static Element getSingleSubElement(Element parentElement, String name) {
    NodeList nodes = parentElement.getElementsByTagName(name);
    if (nodes.getLength() != 1) {
        throw new IllegalArgumentException("Expected the element " + parentElement.getNodeName()
                + " to have one child called " + name + " but found " + nodes.getLength());
    }
    return (Element) nodes.item(0);
}

From source file:Main.java

public static int getSize(Element el, String tagName) {
    NodeList list = el.getElementsByTagName(tagName);
    return list.getLength();
}

From source file:Main.java

public static NodeList getNodeList(Node pNode, String tagName) {
    Element nodeElement = (Element) pNode;
    NodeList theNodeList = nodeElement.getElementsByTagName(tagName);
    return theNodeList;
}

From source file:Main.java

public static Element getElementByTagName(Element element, String name) {
    NodeList nodes = element.getElementsByTagName(name);
    if (nodes.getLength() == 0)
        return null;
    else/*w  w  w.  ja va 2s. com*/
        return (Element) nodes.item(0);
}

From source file:Main.java

public static String getElementValueByTagName(Element root, String tagName) {
    NodeList nodes = root.getElementsByTagName(tagName);
    if (nodes != null) {
        Node node = nodes.item(0).getFirstChild();
        if (node != null) {
            return node.getNodeValue();
        } else {//from  w  w  w.j  a  va  2 s  .c  o m
            return null;
        }
    } else {
        return null;
    }
}

From source file:Main.java

public static Element getChildren(Element element, String childName) {
    NodeList ndLs = element.getElementsByTagName(childName);
    return (Element) ndLs.item(0);
}

From source file:Main.java

public static Element getFirstNode(Element elem, String name) {
    NodeList nl = elem.getElementsByTagName(name);
    if (nl.getLength() > 0)
        return (Element) nl.item(0);
    return null;//from w ww  .  jav  a  2  s  . co  m
}

From source file:Main.java

public static boolean elementExists(Element properties, String name) {
    NodeList nodeList = properties.getElementsByTagName(PROPERTY_NODE);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element element = (Element) nodeList.item(i);
        if (name.equals(element.getAttributes().getNamedItem(NAME_ATTR).getNodeValue()))
            return true;
    }/*ww  w. j  a  v a2  s.  c  om*/
    return false;
}

From source file:Main.java

public static String getChildTagValue(Node node, String tag) {
    Element element = (Element) node;
    NodeList childrenWithTag = element.getElementsByTagName(tag).item(0).getChildNodes();

    Node valueNode = (Node) childrenWithTag.item(0);

    return valueNode.getNodeValue();
}