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 NodeList getTestCases(String fileName) throws Exception {
    Document document = parseData(fileName);
    return document.getElementsByTagName("testcase");

}

From source file:Main.java

/**
 * Set the value of the first tag found with the given name in the given document<br/>. If the tag with the
 * requested name doesn't have a child as textnode with the tagValue is created.
 *
 * @param doc// w  w  w. ja va2  s.c  o m
 * @param tagName
 * @param tagValue
 */
public static void setTagValue(Document doc, String tagName, String tagValue) {
    NodeList tagList = doc.getElementsByTagName(tagName);
    if (tagList.getLength() > 0) {
        if (tagList.item(0).getFirstChild() != null) {
            tagList.item(0).getFirstChild().setNodeValue(tagValue);
        } else {
            tagList.item(0).appendChild(doc.createTextNode(tagValue));
        }
    }
}

From source file:Main.java

static public Element getFirstElementByTagName(Document document, String name) {
    return getFirstElement(document.getElementsByTagName(name));
}

From source file:Main.java

/**
 * Return array of elements from given path.
 * @param doc/*from www.j  a v  a 2  s .c  o m*/
 * @param path      path array ("/root/items/item")
 * @return
 */
public static List<Element> getElements(Document doc, String[] path) {
    NodeList nodes = doc.getElementsByTagName(path[0]);
    return getElements(nodes, path);
}

From source file:Main.java

/**
 * Returns the value of an attribute in the first element in a document with a given tag name.
 * This is useful for well structured documents when it is known that there is only
 * one such element and that it is has that attribute.
 * //  ww  w. j av a  2s  .c om
 * @param document The document to search within.
 * @param tagname The name of the element to access.
 * @param attributename The attribute's name.
 * @return The value of the attribute of the first respective element, or the empty string, if
 * the element of the attribute could not be found.
 */
public static String getAttribute(Document document, String tagname, String attributename) {
    NodeList list = document.getElementsByTagName(tagname);
    if (list.getLength() < 1) {
        return "";
    }
    Element tag = (Element) list.item(0);
    return tag.getAttribute(attributename);
}

From source file:Main.java

private static String getText(Document doc, String tagName, int idx) {
    NodeList nodeList = doc.getElementsByTagName(tagName);
    if (nodeList.getLength() <= 0)
        return "";
    return nodeList.item(idx).getTextContent();
}

From source file:Main.java

/**
 * Returns the text-content of the first element in a document with a given tag name.
 * This is useful for well structured documents when it is known that there is only
 * one such element.//from  w  w w . ja  va2s .  c o m
 * 
 * @param document The document to search within.
 * @param tagname The name of the element to retrieve.
 * @return The text content of the element. Text nodes therein are appended for the result, but
 * no further descendants are included. If the element could not be found, the empty string is
 * returned.
 */
public static String getNodeContent(Document document, String tagname) {
    NodeList list = document.getElementsByTagName(tagname);
    if (list.getLength() < 1) {
        //            log.debug("Not found: " + tagname);
        return "";
    }
    Element tag = (Element) list.item(0);
    NodeList content = tag.getChildNodes();
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < content.getLength(); i++) {
        Node node = content.item(i);
        if (node instanceof Text) {
            buf.append(((Text) node).getNodeValue());
        }
    }
    String textcontent = buf.toString().trim();
    //        log.debug("getNodeContent: " + tagname + " = [" + textcontent + "]");
    return textcontent;
}

From source file:Main.java

public static Node getNode(Document doc, String tagName) {
    NodeList scriptNodes = doc.getElementsByTagName(tagName);
    return scriptNodes.item(0);
}

From source file:Main.java

public static Node findNodeByTagName(Document document, String tagName) {
    Node foundNode = null;//from   w w w. ja va  2 s.com
    NodeList nodes = document.getElementsByTagName(tagName);
    if (nodes.getLength() > 0)
        foundNode = nodes.item(0);
    return foundNode;
}

From source file:Main.java

public static Vector<String> getHandsets(String file) throws Exception {
    Vector<String> vec = new Vector();
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder dombuilder = domfac.newDocumentBuilder();
    FileInputStream is = new FileInputStream(file);

    Document doc = dombuilder.parse(is);
    NodeList nodeList = doc.getElementsByTagName("devices");
    if (nodeList != null && nodeList.getLength() >= 1) {
        Node deviceNode = nodeList.item(0);
        NodeList children = deviceNode.getChildNodes();
        if (children != null && children.getLength() >= 1) {
            for (int i = 0; i < children.getLength(); i++) {
                vec.add(children.item(i).getTextContent());
            }//from  w  ww . j av a 2 s .  co m
        }
    }
    return vec;
}