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 Double get_double(Element element, String name) {
    Double d = null;//from www.  ja v a2 s  .  c om
    NodeList list = element.getElementsByTagName(name);
    if (list.getLength() > 0) {
        try {
            d = Double.valueOf(list.item(0).getTextContent());
        } catch (NumberFormatException e) {
            System.err.println(e);
            return null;
        }
    } else {
        System.err.println("No \"" + name + "\" defined");
        return null;
    }

    return d;
}

From source file:Main.java

/**
 * Checks whether given xml element contains a node of given name
 * @param ele xml element/*from w w w .j  a  v a 2  s .  c o m*/
 * @param tagName name of the node to be found
 * @return true if node is found under xml element, false if not
 */
public static boolean containsNode(Element ele, String tagName) {
    try {
        NodeList nl = ele.getElementsByTagName(tagName);
        if (nl != null && nl.getLength() > 0) {
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

public static String getTagValue(String sTag, Element eElement) {
    String ret = null;/*  w  w w.  ja v  a  2s . c o m*/

    try {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0);
        ret = nValue.getNodeValue();
    } catch (NullPointerException e) {
        // System.t.println("Null pour l'item : " + sTag);
    }
    return ret;
}

From source file:Main.java

/**
 * A helper method for quick property saving/modification. If the property element does not exist, it is automatically created.
 *
 * @param element The parent element.//from   w  ww  .j a va 2  s.  c o m
 * @param name The property name.
 * @param value The property value
 * @return
 */
public static void quickPropertyWrite(Element element, String name, String value) {
    NodeList nodeList = element.getElementsByTagName(name);
    if (nodeList.getLength() == 1) {
        nodeList.item(0).setTextContent(value);
    } else {
        Element newProp = element.getOwnerDocument().createElement(name);
        newProp.setTextContent(value);
        element.appendChild(newProp);
    }
}

From source file:Main.java

/**
 * Gets the immediately child elements list from the parent element.
 *
 * @param parent/*  ww  w  . j  a  va2 s . com*/
 *          the parent element in the element tree
 * @param tagName
 *          the specified tag name
 * @return the NOT NULL immediately child elements list
 */
public static List<Element> getChildElements(Element parent, String tagName) {
    NodeList nodes = parent.getElementsByTagName(tagName);
    List<Element> elements = new ArrayList<>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element && node.getParentNode() == parent) {
            elements.add((Element) node);
        }
    }

    return elements;
}

From source file:Main.java

/**
 * get the value of an Attribute in the Xml Document.
 * finds the first occurance of the parent element and then searches its attributes
 * for the occurance of the attribute and retrieves its value.
 * @param root the root Element.//from w  w  w. j  a  v  a 2 s. co m
 * @param elemName the name of the element to search for.
 * @param att the name of the attribute  to search for.
 * @return String the attribute value or null if not found.
 */
public static String getElementAttribute(Element root, String elemName, String att) {
    NodeList nl = root.getElementsByTagName(elemName);
    if (null == nl) {
        return (null);
    }
    Node n = nl.item(0);
    if (null == n) {
        return (null);
    }
    NamedNodeMap attributes = n.getAttributes();
    if (null == attributes) {
        return (null);
    }
    n = attributes.getNamedItem(att);
    if (null == n) {
        return (null);
    }
    return (n.getNodeValue().trim());

}

From source file:Main.java

static public String getNodeValue(Element parent, String nodeName) {
    NodeList nodes = parent.getElementsByTagName(nodeName);
    if (nodes.getLength() == 0)
        throw new IllegalArgumentException("Cannot find node " + nodeName);
    Node textNode = nodes.item(0).getFirstChild();
    if (textNode != null)
        return (textNode.getNodeValue());
    else//from w  w  w .j a va2 s.  c o  m
        return ("");
}

From source file:Main.java

public static String getTagValue(Element root, String tagName) {
    String returnString = "";
    NodeList list = root.getElementsByTagName(tagName);
    for (int loop = 0; loop < list.getLength(); loop++) {
        Node node = list.item(loop);
        if (node != null) {
            Node child = node.getFirstChild();
            if ((child != null) && (child.getNodeValue() != null)) {
                return child.getNodeValue();
            }//from   w  w w.  j  a  va2 s. c  o m
        }
    }
    return returnString;
}

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
    }//from w w w.j av a2 s  .  com

    return datesArray;
}

From source file:Main.java

public static Element getElement(Element parentElement, String nodeName, String id) {
    NodeList nodeList = parentElement.getElementsByTagName(nodeName);
    int i;/*www.j  a  va  2  s.  c o m*/
    for (i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            if (id.equals(element.getAttribute(idName))) {
                return element;
            }
        }
    }
    return null;
}