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 HashMap<String, Element> buildIndex(Element element, String tagName, String idName) {
    NodeList nl = element.getElementsByTagName(tagName);
    HashMap<String, Element> index = new HashMap<>(nl.getLength());
    for (int i = 0; i < nl.getLength(); i++) {
        Element el = (Element) nl.item(i);
        String id = el.getAttribute(idName);
        if (!id.isEmpty())
            index.put(id, el);/*from w  w  w.ja  v  a 2 s.c  o  m*/
    }
    return index;
}

From source file:Main.java

public static HashMap<String, Element> createIndex(Element root, String tagName, String idName) {
    NodeList nodes = root.getElementsByTagName(tagName);
    HashMap<String, Element> index = new HashMap<>(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        Element el = (Element) nodes.item(i);
        String key = el.getAttribute(idName);
        if (!key.isEmpty())
            index.put(key, el);//w ww.j  a  v a  2 s .c  o m
    }
    return index;
}

From source file:com.codebutler.rsp.Util.java

public static String getChildNodeValue(Element element, String tagName) {
    return element.getElementsByTagName(tagName).item(0).getFirstChild().getNodeValue();
}

From source file:Main.java

/**
 * //from  w ww .j  a va  2  s  .  co  m
 * @param element
 * @param nodeName
 * @return Element
 */
public static Element getFirstElement(final Element element, final String nodeName) {
    final NodeList list = element.getElementsByTagName(nodeName);

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

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

From source file:Main.java

public static void addSimpleOutput(Element success, Document outDoc, String tagName, String value) {
    Element output = (Element) success.getElementsByTagName("output").item(0);
    Element outItem = outDoc.createElement(tagName);
    outItem.setTextContent(value);//from   ww  w  . j  a  v  a  2 s  .  co  m
    output.appendChild(outItem);
}

From source file:Main.java

/**
 * Searches for the element with the specified name in the whole subtree of
 * element.//from   w w  w .  ja  va 2s .co  m
 * 
 * @param element
 * @param localName
 * @return
 */
public static Element findElement(Element element, String localName) {
    NodeList nList = element.getElementsByTagName(localName);
    if (nList.getLength() == 0) {
        return null;
    }
    return (Element) nList.item(0);
}

From source file:Main.java

public static Element getElementWithKeyFromElement(final Element element, final String key) {
    final NodeList nodeList = element.getElementsByTagName(key);
    Element result = null;//from w  w  w .ja va2s . c  o m
    if (nodeList.getLength() > 0) {
        result = (Element) nodeList.item(0);
    }
    return result;
}

From source file:Main.java

/**
 * sets the value of an Element in the Xml Document.
 * finds the first occurance of the element and sets its value.
 * @param root the root Element./*w  w  w.  ja  v  a 2s  . com*/
 * @param elemName the name of the element to search for.
 * @return String the element value or null if not found.
 */
public static void setNodeValue(Document doc, String elemName, String value) {
    Element root = doc.getDocumentElement();
    NodeList nl = root.getElementsByTagName(elemName);
    if (null != nl) {
        Node n = nl.item(0);
        if (null != n) {
            n.getFirstChild().setNodeValue(value);
        } else {
            addNode(doc, elemName, value);
        }
    } else {
        addNode(doc, elemName, value);
    }

}

From source file:Main.java

public static NodeList getNodeList(Element element, String tagName) {
    return element.getElementsByTagName(tagName);
}

From source file:Main.java

public static List<Element> elements(Element element, String tagName) {
    NodeList nodeList = element.getElementsByTagName(tagName);
    List<Element> elements = new java.util.ArrayList<Element>(nodeList.getLength());
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node child = nodeList.item(i);
        if (child.getParentNode() == element) {
            elements.add((Element) child);
        }//w ww.j a v  a2 s.com
    }
    return elements;
}