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 int getNodeSizeFromXmlStr(String xmlStr, String nodeName) {
    int size = 0;
    xmlStr = xmlStr.trim();//ww  w.  j ava2  s  .co m
    Element element = getRootNodeFromXmlStr(xmlStr);
    size = element.getElementsByTagName(nodeName).getLength();
    return size;
}

From source file:Utils.java

public static String getElementStringValue(Document document, Element parent, String element) {
    NodeList nl = parent.getElementsByTagName(element);
    if (nl.getLength() == 0) {
        return "";
    }//from   w w  w.j  av a  2 s . co  m

    Node n = nl.item(0).getFirstChild();
    if (n == null) {
        return "";
    }

    return n.getNodeValue();
}

From source file:Main.java

/**
 * Restituisce il primo sottoelemeto di xmlRoot trovato con il nome
 * specificato//  w ww  .  j av  a  2  s.c  o  m
 * 
 * @param xmlRoot
 *            l'elemento in cui cercare (NOT null)
 * @param name
 *            il nome del sottoelemento da restituire
 * @return il primo sottoelemento trovato o null se non presente
 */
public static Element getSingleElement(Element xmlRoot, String name) {
    NodeList nodeList = xmlRoot.getElementsByTagName(name);
    Element element = null;
    if (nodeList != null && nodeList.getLength() > 0) {
        element = (Element) nodeList.item(0);
    }
    return element;
}

From source file:Main.java

public static Element getFirstElement(Element element, String elementName) throws IOException {
    NodeList list = element.getElementsByTagName(elementName);
    if (list.getLength() >= 1) {
        Element subElement = (Element) list.item(0);
        return subElement;
    } else {// w  w  w . j a  v  a 2  s  .co  m
        return null;
    }
}

From source file:Main.java

public static String GetElementText(Element parent, String node_name) {
    NodeList match = parent.getElementsByTagName(node_name);
    if (match.getLength() == 0)
        return null;
    Node m = match.item(0);//from   ww  w. j av a2 s.  co  m
    return getNodeText(m);
}

From source file:Main.java

/**
 * This method returns the child element with the particular name.
 * @param parentElement The parent element.
 * @param tagName The child element name.
 * @return The child element.//from   w w w .  j  av a  2 s  .c om
 */
public static Element getChildElement(Element parentElement, String tagName) {
    NodeList nodes = parentElement.getElementsByTagName(tagName);
    if (nodes != null && nodes.getLength() != 0) {
        return (Element) nodes.item(0);
    } else {
        return null;
    }
}

From source file:Main.java

public static String getTextValue(Element ele, String tagName) {
    String textVal = null;//  ww  w.  j  a va2 s .c om
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        if (el == null)
            return null;
        if (el.getFirstChild() == null)
            return null;
        textVal = el.getFirstChild().getNodeValue();
    }

    return textVal;
}

From source file:Main.java

public static List<Element> getElements(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) {
            elements.add((Element) node);
        }/*from www  . j a  va2 s . c  o m*/
    }

    return elements;
}

From source file:Main.java

/**
 * An Iterable for the Element's childs, with a particular name, of a node
 *
 * @param n the node get the children from
 * @param elementName the name of the child elements
 * @return An Iterable for the Element's children, with a particular name,
 * of a node/*from  w  w  w. j  a v a2s.c  om*/
 */
public static Iterable<Element> elements(Element n, String elementName) {
    NodeList subNodes = n.getElementsByTagName(elementName);
    int sz = subNodes.getLength();
    ArrayList<Element> elements = new ArrayList<>(sz);
    for (int idx = 0; idx < sz; idx++) {
        Node node = subNodes.item(idx);
        elements.add((Element) node);
    }
    return elements;
}

From source file:Main.java

/**
 * An Iterable for the Element's childs, with a particular name, of a node
 * //w  ww.  j a  v  a  2 s .  c  o m
 * @param n
 *            the node get the children from
 * @param elementName
 *            the name of the child elements
 * @return An Iterable for the Element's children, with a particular name, of a node
 */
public static Iterable<Element> elements(Element n, String elementName) {
    NodeList subNodes = n.getElementsByTagName(elementName);
    int sz = subNodes.getLength();
    ArrayList<Element> elements = new ArrayList<Element>(sz);
    for (int idx = 0; idx < sz; idx++) {
        Node node = subNodes.item(idx);
        elements.add((Element) node);
    }
    return elements;
}