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 List<Element> getChildren(Element element, String tagName) {
    NodeList nodes = element.getElementsByTagName(tagName);

    List<Element> elements = new ArrayList<>(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        elements.add((Element) nodes.item(i));
    }/*  ww  w  . j  a va 2  s  . c  om*/
    return elements;
}

From source file:Main.java

public static Node getElementByTagName(Element element, String tagname) {

    NodeList nodeList = element.getElementsByTagName(tagname);

    if (nodeList != null && nodeList.getLength() > 0) {

        return nodeList.item(0);
    } else {//from   w  w w .  ja  v  a2  s. co  m

        return null;
    }
}

From source file:Main.java

public static Element getChildElement(Element p_rootElement, String p_elementName) {
    NodeList nodeList = p_rootElement.getElementsByTagName(p_elementName);
    // if (nodeList.getLength() >= 0)
    return (Element) nodeList.item(0);
    // return null;
}

From source file:Main.java

public static List<String> getElementTextList(final Element eElement, final String name) {
    NodeList nodeList = eElement.getElementsByTagName(name);
    if (nodeList != null && nodeList.getLength() > 0) {
        int length = nodeList.getLength();
        List<String> nodeItems = new ArrayList<String>();
        for (int index = 0; index < length; ++index) {
            nodeItems.add(nodeList.item(index).getTextContent().trim());
        }/*from  w ww  . j a  v a 2  s . c  o  m*/
        return nodeItems;
    }

    return Collections.EMPTY_LIST;
}

From source file:Main.java

public static String getFirstChildNodeAttributeValue(Element parent, String nodeName) {
    return parent.getElementsByTagName(nodeName).item(0).getFirstChild().getNodeValue();
}

From source file:Main.java

/**
 *
 * @param document//from  www .j  a  va 2 s.  com
 * @param tagName
 * @return
 */
public static String getTagValueAsString(Document document, String tagName) {
    if (document == null || tagName == null || tagName.length() == 0)
        return null;

    String tagValue = null;
    NodeList nlist = null;
    Element element = null;

    Element root = document.getDocumentElement();

    nlist = root.getElementsByTagName(tagName);
    element = (Element) nlist.item(0);
    if (element.hasChildNodes()) {
        tagValue = element.getFirstChild().getNodeValue();
    }
    return tagValue;
}

From source file:Main.java

public static Element getElement(Element item, String tag) {
    NodeList nodes = item.getElementsByTagName(tag);
    if (nodes != null && nodes.getLength() > 0) {

        Element element = (Element) nodes.item(0);

        String nodeName = null;//from ww w  .  ja v  a 2 s.  c o  m
        if (element != null) {
            nodeName = element.getNodeName();
        }

        if (nodeName != null && nodeName.compareTo(tag) == 0) {
            return element;
        }
    }

    return null;
}

From source file:Main.java

private static String getTextValue(Element ele, String tagName) {
    String textVal = null;//w w  w .  java 2s  .c o  m
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        textVal = el.getFirstChild().getNodeValue();
    }

    return textVal;
}

From source file:Main.java

public static Element nthElement(Element parent, String elementName, int index) {
    NodeList nds = parent.getElementsByTagName(elementName);
    if (nds.getLength() < index)
        throw new NoSuchElementException();
    Element node = (Element) nds.item(index - 1);
    return node;/*from w ww.  j a  v  a2  s. c om*/
}

From source file:Main.java

public static List<Element> getChildElements(Element parent, String tagName) {
    NodeList nodes = parent.getElementsByTagName(tagName);
    ArrayList elements = new ArrayList();

    for (int i = 0; i < nodes.getLength(); ++i) {
        Node node = nodes.item(i);
        if (node instanceof Element && node.getParentNode() == parent) {
            elements.add((Element) node);
        }/*w  ww  .jav a  2s  .c  o  m*/
    }

    return elements;
}