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 getFirstElementByTagName(Element parentElement, String tagName) {
    NodeList list = parentElement.getElementsByTagName(tagName);
    if (list.getLength() == 0)
        return null;
    else// ww w  .j a  v  a2 s .co m
        return (Element) list.item(0);
}

From source file:Main.java

public static String getFirstElementAttr(Element parent, String tag, String attr) {
    NodeList nl = parent.getElementsByTagName(tag);
    if (nl.getLength() > 0) {
        return ((Element) nl.item(0)).getAttribute(attr);
    }//from   ww  w. j  av a 2 s  .c o m
    return null;
}

From source file:Main.java

public static Element getFirstChildElement(Element parent, String childName) {
    if (parent.getElementsByTagName(childName).getLength() > 0) {
        return (Element) parent.getElementsByTagName(childName).item(0);
    }/*  w w w. ja va2 s  .  c  o  m*/
    return null;
}

From source file:Main.java

public static Node getTag(Element root, String tagName) {
    NodeList list = root.getElementsByTagName(tagName);
    for (int loop = 0; loop < list.getLength(); loop++) {
        Node node = list.item(loop);
        System.out.println("nodeName : " + node.getNodeName() + ":::::" + node.getNodeType());
        if (node.getNodeName().equals(tagName)) {
            return node;
        }//from   ww  w.  ja v a2s . c om
    }
    return null;
}

From source file:Main.java

/**
 * Return the child element whose name is as specified. 
 * <p>/*  w ww. j  a v  a  2 s .  c om*/
 * Note: it's up to the user to guarantee that there EXISTS and is only 
 * ONE child element.
 * @param parent
 * @param name
 * @return
 */
public static Element getUniqueElementByTagName(Element parent, String name) {
    Element elm = (Element) parent.getElementsByTagName(name).item(0);
    return elm;
}

From source file:Main.java

public static String getTag(org.w3c.dom.Element e, String index) {
    NodeList nList = e.getElementsByTagName(index);
    if (nList.getLength() != 0) {
        Node nNode = nList.item(0);
        return nNode.getTextContent();
    }//from w  ww. j av a 2s  .  c  om
    return null;
}

From source file:Main.java

public static Element getElementByTagName(Element element, String tagName) {
    NodeList nodeList = element.getElementsByTagName(tagName);
    if (nodeList.getLength() == 0) {
        return null;
    }/*from  w  w  w.  j  a  v a  2 s.  c  om*/
    Node node = nodeList.item(0);
    return (Element) node;
}

From source file:Main.java

/**
 * get the value of an Element in the Xml Document.
 * finds the first occurance of the element and retrieves its value.
 * @param root the root Element./*from  ww  w  .j a v a2  s .  co m*/
 * @param elemName the name of the element to search for.
 * @return String the element value or null if not found.
 */
public static String getElementValue(Element root, String elemName) {
    NodeList nl = root.getElementsByTagName(elemName);
    if (null == nl) {
        return (null);
    }
    Node n = nl.item(0);
    return (n.getFirstChild().getNodeValue().trim());
}

From source file:Main.java

public static List<Element> getChildElements(Element elem, String name) {
    NodeList nodes = elem.getElementsByTagName(name);
    List<Element> elements = new ArrayList<Element>(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) {
            elements.add((Element) node);
        }/*from   ww  w . j ava  2 s.c o  m*/
    }
    return elements;
}

From source file:Main.java

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