Example usage for org.w3c.dom Document getElementsByTagName

List of usage examples for org.w3c.dom Document getElementsByTagName

Introduction

In this page you can find the example usage for org.w3c.dom Document getElementsByTagName.

Prototype

public NodeList getElementsByTagName(String tagname);

Source Link

Document

Returns a NodeList of all the Elements in document order with a given tag name and are contained in the document.

Usage

From source file:Main.java

public static Element getLastElement(Document dom, String tag) {
    NodeList nl = dom.getElementsByTagName(tag);

    int size = nl.getLength();
    if (size == 0) {
        return null;
    }/*  w  w  w .  ja  v a  2s  .c  o  m*/

    return (Element) nl.item(size - 1);
}

From source file:Main.java

public static String getFirstChildValue(String childName, Document parent) {
    return getElementValue(parent.getElementsByTagName(childName).item(0));
}

From source file:Main.java

public static Element getElementByTagName(Document doc, String eleName) {

    NodeList nl = doc.getElementsByTagName(eleName);
    if (null == nl) {
        return null;
    }/*from   www . j  ava 2  s.co  m*/
    Node item = nl.item(0);
    return (Element) item;
}

From source file:Main.java

public static String getFirstElementText(Document parent, String tag) {
    NodeList nl = parent.getElementsByTagName(tag);
    if (nl.getLength() > 0) {
        return ((Element) nl.item(0)).getTextContent();
    }/*from w w w.  j  a  v  a 2  s  .  c om*/
    return null;
}

From source file:Main.java

public static String getNodeContent(Document document, String nodeName) {
    NodeList nl = document.getElementsByTagName(nodeName);
    if (0 == nl.getLength()) {
        return "";
    } else {//  w ww .ja va  2 s  .  c o m
        return nl.item(0).getTextContent();
    }
}

From source file:Main.java

public static Node GetSimpleTagNode(Document doc, String tag) {
    NodeList nList2 = doc.getElementsByTagName(tag);
    for (int temp = 0; temp < nList2.getLength(); temp++) {
        Node nNode2 = nList2.item(temp);
        if (nNode2.getNodeType() == Node.ELEMENT_NODE) {
            return nNode2;
        } // end if nnode.getnodetype()
    } //end for int temp
    return null;//  ww w . j  a v  a2  s.com
}

From source file:Main.java

/**
 * Count Elements in Document by Tag Name
 * @param tag//from ww w.j  a v  a  2  s .c  o m
 * @param document
 * @return number elements by Tag Name
 */
public static int countByTagName(String tag, Document document) {
    NodeList list = document.getElementsByTagName(tag);
    return list.getLength();
}

From source file:Main.java

public static Element getElementByTagName(Document document, String element) {
    NodeList nodeList = document.getElementsByTagName(element);
    if (nodeList.getLength() > 0) {
        return (Element) nodeList.item(0);
    } else {/*from  ww  w.j  a  v  a  2s  .co  m*/
        return null;
    }
}

From source file:Main.java

/**
 * Get the value of the first tag found with the given name in the given document<br/>.
 *
 * @param doc//from ww  w  .  j  ava  2  s . c om
 * @param tagName
 * @return the value of the node or the empty string, if no node with this name exits in this document
 */
public static String getTagValue(Document doc, String tagName) {
    NodeList tagList = doc.getElementsByTagName(tagName);
    if (tagList.getLength() > 0) {
        return tagList.item(0).getFirstChild().getNodeValue().trim();
    }
    return "";
}

From source file:Main.java

/**
 * @param document/*  w  ww.  j a  v a 2  s  .c o  m*/
 * @param name
 * @return
 */
public static String getElementByName(Document document, String name) {
    NodeList nodes = document.getElementsByTagName(name);
    String s = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        s = node.getTextContent().trim();
    }
    return s;
}