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 List<Element> getElementsByTagName(Element element, String name) {

    List<Element> elements = new LinkedList<Element>();
    NodeList nodeList = element.getElementsByTagName(name);

    for (int i = 0; i < nodeList.getLength(); i++) {
        elements.add((Element) nodeList.item(i));
    }/*from  w w w  .j  a  v  a2s.c o  m*/

    return elements;
}

From source file:Utils.java

public static Element findElementAndSetElseCreateAndSet(Document document, Element parent, String child,
        String value) {//w ww  . j ava  2  s  .co  m
    NodeList nl = parent.getElementsByTagName(child);
    if (nl.getLength() == 0) {
        parent.appendChild(document.createElement(child));
    }
    Element ret = (Element) parent.getElementsByTagName(child).item(0);
    if (ret.getFirstChild() != null) {
        ret.removeChild(ret.getFirstChild());
    }
    ret.appendChild(document.createTextNode(value));
    return ret;
}

From source file:Main.java

/**
 * This method extracts the list of elements from the @param element that contains the @param tagName.
 *
 * @param tagName the tag name used to get the list of elements
 * @param element the dom element to look for the tags with the given name
 * @return list of elements that have the @param tagName
 *//*  ww  w  .ja  v  a  2 s  .  co m*/
public static List<Element> getElementsByTagName(String tagName, Element element) {
    List<Element> elements = new ArrayList<>();

    NodeList nodeList = element.getElementsByTagName(tagName);

    if (nodeList != null && nodeList.getLength() > 0) {

        for (int index = 0; index < nodeList.getLength(); index++) {
            Node currentNode = nodeList.item(index);

            if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                elements.add((Element) currentNode);
            }
        }
    }
    return elements;
}

From source file:Main.java

public static List<Element> getChildElements(Element parent, Enum<?> child) {
    final String elName = getXmlName(child);
    final NodeList list = parent.getElementsByTagName(elName);
    final List<Element> children = new ArrayList<Element>(list.getLength());
    for (int i = 0; i < list.getLength(); i++) {
        final Element elChild = (Element) list.item(i);
        children.add(elChild);//from  w ww.  j a  va2s.c  o  m
    }
    return children;
}

From source file:Main.java

/**
 * Return an iterator for the subelements.
 * @return java.util.Iterator/*from  w  w  w  .  j  a v  a 2  s.  c om*/
 * @param element org.w3c.dom.Element
 * @param name java.lang.String
 */
public static Iterator getNodeIterator(Element element, String name) {
    List<Node> list = new ArrayList<Node>();
    NodeList nodeList = element.getElementsByTagName(name);

    int length = nodeList.getLength();
    for (int i = 0; i < length; i++)
        list.add(nodeList.item(i));

    return list.iterator();
}

From source file:Main.java

public static Element getChildByTagName(Element e, String tagname) {
    if (e == null) {
        return null;
    }/*from   ww  w  .ja va 2  s. c om*/

    NodeList children = e.getElementsByTagName(tagname);

    if ((children == null) || (children.getLength() == 0)) {
        return (null);
    }

    return ((Element) children.item(0));
}

From source file:Main.java

public static ArrayList<Element> getElements(String elementName, Element element) {
    ArrayList<Element> result = new ArrayList<Element>();
    NodeList list = element.getElementsByTagName(elementName);

    for (int i = 0; i < list.getLength(); i++) {
        Node nNode = list.item(i);

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            result.add((Element) nNode);
        }/* w w w.j  a v  a 2  s  . c o  m*/
    }
    return result;
}

From source file:Main.java

/** @param text <code>null</code> removes 'subElement' completely */
public static Text setTextField(Element node, String subElement, String text) {
    NodeList nl = node.getElementsByTagName(subElement);
    if (nl == null || nl.getLength() == 0) {
        if (text == null)
            return null;
        Document d = node.getOwnerDocument();
        Element e = d.createElement(subElement);
        Text ans = d.createTextNode(text);
        e.appendChild(ans);/*w w  w. j  a  v  a  2s .  c  om*/
        node.appendChild(e);
        return ans;
    } else if (text != null) {
        return setText(nl.item(0), text);
    } else {
        node.removeChild(nl.item(0));
        return null;
    }
}

From source file:Main.java

public static Element getChildElement(Element parent, String childTagName) {

    Element el = null;/*from w  ww .  j a  v  a 2  s .c om*/

    NodeList nl = parent.getElementsByTagName(childTagName);

    if (nl != null && nl.getLength() > 0) {
        el = (Element) nl.item(0);
    }

    return el;

}

From source file:Main.java

public static void addParameter(Document outDoc, Element succFail, String name, String value) {
    //System.out.println("value:" + value);
    Element parameters = (Element) succFail.getElementsByTagName("parameters").item(0);
    Element parameter = outDoc.createElement(name);
    parameter.setAttribute("value", value);
    parameters.appendChild(parameter);//from w  ww  .  j a  v a  2  s .c o  m
}