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 String getTextTag(Node node, String tagName) {
    Element elem = (Element) node;
    NodeList list = elem.getElementsByTagName(tagName);
    if (list.getLength() == 0)
        return null;
    Node child = list.item(0).getFirstChild();
    if (child.getNodeType() == Node.TEXT_NODE)
        return child.getNodeValue();
    return null;/*from   w w  w  .  j  a va  2  s  . com*/
}

From source file:Main.java

public static Element getChildElement(Element parent, String childName) {
    NodeList list = parent.getElementsByTagName(childName);

    Node node;//w  w w . ja  va2 s .c om
    for (int i = 0; i < list.getLength(); i++) {
        node = list.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) node;
        }
    }
    return null;
}

From source file:Main.java

public static Element getElement(Element element, String tagName) {
    final NodeList nodeList = element.getElementsByTagName(tagName);
    if (nodeList == null || nodeList.getLength() < 1) {
        return null;
    }//from   w w w.  j  a va2  s . c  o m
    int length = nodeList.getLength();
    for (int i = 0; i < length; i++) {
        Element childEle = (Element) nodeList.item(i);
        if (childEle == null || childEle.getParentNode() == element) {
            return childEle;
        }
    }
    return null;
}

From source file:Main.java

public static List<Element> getElements(Element element, String tagName) {
    final NodeList nodeList = element.getElementsByTagName(tagName);
    return transElements(element, nodeList);
}

From source file:Main.java

/**
 * //from  www  .jav a2 s  .  c o  m
 * @param dataRoot
 *            the starting node
 * @param name
 *            the name of the subelement to find
 * @return the list of all DOM Element with the provided name direct child
 *         of the starting node
 */
public static List<Element> getElementList(Element dataRoot, String name) {
    NodeList list = dataRoot.getElementsByTagName(name);
    List<Element> listElements = new ArrayList<Element>();
    for (int i = 0; i < list.getLength(); i++) {
        Element item = (Element) list.item(i);
        if (item.getParentNode().equals(dataRoot)) {
            listElements.add(item);
        }
    }
    return listElements;
}

From source file:Main.java

public static String getNodeText(Node parentNode, String nodeName) {
    Element fstElmnt = (Element) parentNode;
    NodeList fstNmElmntLst = fstElmnt.getElementsByTagName(nodeName);
    Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
    NodeList fstNm = fstNmElmnt.getChildNodes();
    String ret = fstNm.item(0).getNodeValue();
    return ret;//  ww w. ja  va2 s  .c  om
}

From source file:Main.java

private static Element findElement(Element root, String tag) throws IOException {
    NodeList elements = root.getElementsByTagName(tag);

    if (elements.getLength() == 0) {
        throw new IOException("Tag " + tag + " was expected and not found.");
    } else if (elements.getLength() != 1) {
        throw new IOException("Tag " + tag + " cannot have multiple definitions.");
    }/*  ww  w.  ja  v a2 s  . co  m*/

    return (Element) elements.item(0);
}

From source file:Main.java

public static Element selectSingleElement(Element parent, String elementName) {
    NodeList list = parent.getElementsByTagName(elementName);

    if (list.getLength() > 0) {
        return (Element) list.item(0);
    }//from w  ww . j a  va  2  s.  c  om

    return null;
}

From source file:Main.java

public static List<Element> getElementList(Element dataRoot, String name) {
    NodeList list = dataRoot.getElementsByTagName(name);
    List<Element> listElements = new ArrayList<Element>();
    for (int i = 0; i < list.getLength(); i++) {
        listElements.add((Element) list.item(i));
    }/*ww  w . ja va  2  s  . c o  m*/
    return listElements;
}

From source file:Main.java

public static String getTagValue(Node node, String name) {

    if (node.getNodeType() == Node.ELEMENT_NODE) {

        Element el = (Element) node;
        NodeList nodeList = el.getElementsByTagName(name);
        if (nodeList.getLength() > 0) {
            el = (Element) nodeList.item(0);
            nodeList = el.getChildNodes();

            if (nodeList.getLength() > 0) {
                return ((Node) nodeList.item(0)).getNodeValue();
            }/*ww  w .  j a  v a2  s .  c om*/
        }

    }
    return "";
}