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 getNode(Element e, String name) {
    Node n = e.getElementsByTagName(name).item(0);
    if (n == null)
        return e;
    else if (n.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) n;
    } else/*w  w w.j  av  a 2  s.c o m*/
        return e;
}

From source file:Main.java

public static String getElement(Element e, String name) {
    if (e.getElementsByTagName(name) == null)
        return "";
    if (e.getElementsByTagName(name).item(0) == null)
        return "";
    return e.getElementsByTagName(name).item(0).getTextContent();
}

From source file:Main.java

/**
 * Fetches text from a element/*from w w w . j a  va  2s.  com*/
 * 
 * @param parent
 *            the parent of the element you want to fetch text from
 * @param tagName
 *            name of the element you want to fetch text from
 * @return text of tag
 */
public static String getText(Element parent, String tagName) {
    return parent.getElementsByTagName(tagName).item(0).getTextContent();
}

From source file:Main.java

public static String getElementText(final Element eElement, final String name) {
    return eElement.getElementsByTagName(name).item(0).getTextContent().trim();
}

From source file:Main.java

public static org.w3c.dom.Element getFirstElement(String name, org.w3c.dom.Element root) {
    NodeList list = root.getElementsByTagName(name);
    if (list != null) {
        return (org.w3c.dom.Element) list.item(0);
    } else//  w w  w .  j  a  v  a  2  s  . c  o m
        return null;
}

From source file:Main.java

public static Node getElement(final Element eElement, final String name) {
    return eElement.getElementsByTagName(name).item(0);
}

From source file:Main.java

public static Element getChildElement(Element parent, String child) {
    return (parent.getElementsByTagName(child) != null) ? (Element) parent.getElementsByTagName(child).item(0)
            : null;/* ww w .j  a  va 2 s.  com*/
}

From source file:Main.java

public static String getChildAsString(Element e, String child) {
    NodeList nodes = e.getElementsByTagName(child);
    if (nodes.getLength() > 0) {
        Element i = (Element) nodes.item(0);
        return i.getTextContent().trim();
    } else/*w ww  .  ja v a 2 s . c  o  m*/
        return null;
}

From source file:Main.java

/**
 * Gets tag value from XML entry./*from ww  w  .  j  a v  a2 s  . c o  m*/
 * @param tag         XML tag String.
 * @param xmlElement   XML Element Object.
 * @return            Integer.parseInt(((Node) lastNodeList.item(0)).getNodeValue()).
 */
public static String getEntry(String tag, Element xmlElement) {
    NodeList nodeList = xmlElement.getElementsByTagName(tag);
    Element element = (Element) nodeList.item(0);
    NodeList lastNodeList = element.getChildNodes();
    return lastNodeList.item(0).getNodeValue();
}

From source file:Main.java

/**Returns the first ELEMENT with name str, while ignoring whitespace nodes
 * //www  . java2 s .  com
 * @param elm 
 * @param str
 * @return the String value of a child node of Element elm that has a name str
 * or null if no match
 */
public static Element getChildElementByName(Element elm, String str) {
    NodeList nList = elm.getElementsByTagName(str);
    return nList == null ? null : (Element) nList.item(0);
}