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 boolean hasElement(Document inDoc, String elementName) {
    NodeList list = inDoc.getElementsByTagName(elementName);
    return (list != null) && (list.getLength() > 0);
}

From source file:Main.java

public static ArrayList<String> getDatesArray(Document allDoc) {
    NodeList nodes = allDoc.getElementsByTagName("day");
    ArrayList<String> datesArray = new ArrayList<String>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Element element = (Element) nodes.item(i);
        NodeList datesNodes = element.getElementsByTagName("date");

        Element line = (Element) datesNodes.item(0);

        datesArray.add(getCharacterDataFromElement(line).trim());//gotta trim...or you can't see shiat
    }/* ww w. jav  a 2 s . co m*/

    return datesArray;
}

From source file:Main.java

public static Node getFirstElementByTagName(Document doc, String tagName) {
    NodeList list = doc.getElementsByTagName(tagName);
    if (list.getLength() > 0) {
        return list.item(0);
    } else {//  w w w  .  java2  s  .  co  m
        return null;
    }
}

From source file:Main.java

public static Node findByID(Document doc, String id, String tagName) {
    NodeList node = doc.getElementsByTagName(tagName);
    for (int i = 0; i < node.getLength(); i++) {
        NamedNodeMap attributtes = node.item(i).getAttributes();
        for (int j = 0; j < attributtes.getLength(); j++)
            if (id.equalsIgnoreCase(attributtes.item(j).getNodeValue()))
                return node.item(i);
    }/* w w  w .  j av a  2 s. c om*/

    return null;
}

From source file:Main.java

public static String getFirstChildNodeValue(Document parent, String nodeName) {
    return parent.getElementsByTagName(nodeName).item(0).getFirstChild().getNodeValue();
}

From source file:Main.java

public static Element loadFirstElementThatMatches(String element, String filename) throws Exception {
    Document doc = loadXml(filename);
    return (Element) doc.getElementsByTagName(element).item(0);
}

From source file:Main.java

/**
 * Method to remove all children of g:options - element
 * /*from www  .jav a2s.  co m*/
 * @param doc document with children  g:options
 * @return document without children g:options
 */
public static Document removeAllOptions(Document doc) {
    NodeList nodes = doc.getElementsByTagName("g:options");
    Node node = nodes.item(0);
    while (node.hasChildNodes())
        node.removeChild(node.getFirstChild());
    return doc;
}

From source file:Main.java

/** Takes the parsed XML representation (i.e. Document) and a string (e.g <variable> in a <query> message)
 * @param document - the parsed XML represenation
 * @param tagName - the name of the data item whose value is desired
 * @return The string that is that value*/
public static String GetValueForNode(Document document, String tagName) {
    NodeList nodes = document.getElementsByTagName(tagName);
    if (nodes == null)
        return (null);
    else {//from  w  ww.  j  a  va 2s. c om
        Node n = nodes.item(0);
        String value = n.getFirstChild().getNodeValue();
        if (value == null)
            return null;
        else
            return value.trim();
    }
}

From source file:Main.java

public static void forEachByTagName(Document doc, String tagname, Consumer<Node> action) {
    NodeList nl = doc.getElementsByTagName(tagname);
    forEach(nl, action);//from w ww. j  a va 2s . c o  m
}

From source file:Main.java

public static void deleteElement(Document fromDoc, String elementName) {
    NodeList list = fromDoc.getElementsByTagName(elementName);
    if ((list != null) && (list.getLength() > 0)) {
        Element element = (Element) list.item(0);
        element.getParentNode().removeChild(element);
    }//ww w. ja va 2s  .  co m
}