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

/**
 * Gets the first child Element with a given name
 * /*from  ww w  .j a  va2s  .  c o  m*/
 * @param n
 *            the node get the children from
 * @param elementName
 *            the name of the child elements
 * @return the first child Element with a given name
 */
public static Element getElementByTagName(Element n, String elementName) {
    NodeList subNodes = n.getElementsByTagName(elementName);
    int sz = subNodes.getLength();
    if (sz > 0)
        return (Element) subNodes.item(0);
    return null;
}

From source file:Main.java

/**
 * Gets the value of a named child element.
 *
 * @param element The parent element./*from w  w w.ja  v  a  2  s  . c  o  m*/
 * @param name The name of the child element to retrieve the value from.
 * @return The text value of the child element.
 */
public static String getElementValue(Element element, String name) {
    return ((Element) element.getElementsByTagName(name).item(0)).getFirstChild().getNodeValue();
}

From source file:Main.java

static boolean hasChildNode(Element root, String childNodeName) {
    NodeList nl = root.getElementsByTagName(childNodeName);
    return (nl.getLength() > 0);
}

From source file:Main.java

/**
 * Gets the first child Element with a given name
 *
 * @param n the node get the children from
 * @param elementName the name of the child elements
 * @return the first child Element with a given name
 *//*from   w  w  w  .  ja v a 2 s. co m*/
public static Element getElementByTagName(Element n, String elementName) {
    NodeList subNodes = n.getElementsByTagName(elementName);
    int sz = subNodes.getLength();
    if (sz > 0) {
        return (Element) subNodes.item(0);
    }
    return null;
}

From source file:Main.java

public static Element getElement(Element root, String name) throws Exception {
    NodeList elements = root.getElementsByTagName(name);
    if (elements.getLength() > 0) {
        return (Element) elements.item(0);
    }//from ww w . j  a v  a2  s. c o m
    throw new Exception("Child element '" + name + "' to parent " + root.getNodeName() + " not found.");
}

From source file:Main.java

/**
 * This method is used to search all the <code>Element</code> objects with
 * given key in the passed <code>Element</code> object.
 * /*from w ww . j  a v  a  2  s . c  om*/
 * @param tagName
 *            Name of the tag as String.
 * @param input
 *            Element object.
 * @return <code>Element[]</code> Returns the array of elements, or an empty
 *         array in case here is no match.
 */
public static List<Element> getElements(String tagName, Element input) {
    NodeList nodes = input.getElementsByTagName(tagName);

    int len = nodes.getLength();
    List<Element> elt = new ArrayList<Element>(len);
    Node node;
    for (int i = 0; i < len; i++) {
        node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            elt.add((Element) node);
        }
    }

    return elt;
}

From source file:Main.java

public static NodeList readXML(File xmlfile, String tag) throws Exception {
    NodeList list = null;/*from w  w w .ja v a2s.c o m*/
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(xmlfile);
    Element rootElement = doc.getDocumentElement();
    list = rootElement.getElementsByTagName(tag);

    return list;
}

From source file:TestDOM.java

public static Element getFirstElement(Element element, String name) {
    NodeList nl = element.getElementsByTagName(name);
    if (nl.getLength() < 1)
        throw new RuntimeException("Element: " + element + " does not contain: " + name);
    return (Element) nl.item(0);
}

From source file:Main.java

public static String getElementValue(Element parent, String label) {
    Element e = (Element) parent.getElementsByTagName(label).item(0);

    try {//from w  ww.ja v  a  2  s.c om
        Node child = e.getFirstChild();
        if (child instanceof CharacterData) {
            CharacterData cd = (CharacterData) child;
            return cd.getData();
        }
    } catch (Exception ex) {
    }
    return "";
}

From source file:Main.java

public static Node getNode(Node node, String nodeName) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {

        Element el = (Element) node;
        NodeList nodeList = el.getElementsByTagName(nodeName);
        if (nodeList.getLength() > 0) {
            return nodeList.item(0);
        }//ww w.  j a  v  a 2 s.  c  o m

    }

    return null;
}