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

/**
 * This method return role name from xml file.
 * @param element String- privilege Element
 * @param elementName -Element name for which value has to be return
 * @return String Role name/* w w w.ja v  a 2s .co  m*/
 */
public static String getElementValue(Element element, String elementName) {
    String roleName = "";
    NodeList elementList = element.getElementsByTagName(elementName);
    Element ele = (Element) elementList.item(0);
    NodeList valueNodeList = ele.getChildNodes();
    Node node = ((Node) valueNodeList.item(0));
    if (node != null) {
        roleName = node.getNodeValue();
    }
    return roleName;
}

From source file:Main.java

public static Element getChildElement(Element el, String tagName) {
    if (el == null)
        return null;
    NodeList list = el.getElementsByTagName(tagName);
    if (list != null && list.getLength() > 0) {
        return (Element) list.item(0);
    }/*  w  ww  .  j  a  va  2s  .c  o  m*/
    return null;
}

From source file:Main.java

/**
 * Return list of content Strings of all child elements with given tag name.
 * @param elem/* w w w .j a v  a 2s .c  o m*/
 * @param childTagName
 * @return List 
 */
public static List getChildTextList(Element elem, String childTagName) {
    NodeList nodeList = elem.getElementsByTagName(childTagName);
    int len = nodeList.getLength();
    if (len == 0) {
        return Collections.EMPTY_LIST;
    }

    List list = new ArrayList(len);
    for (int i = 0; i < len; i++) {
        list.add(getElementText((Element) nodeList.item(i)));
    }
    return list;

}

From source file:Main.java

private static String getValue(Element ele, String tagName) {
    String value = null;/*from  ww w  .  j a  va 2  s.co  m*/
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        value = el.getFirstChild().getNodeValue();
    }

    return value;
}

From source file:Main.java

public static Element getElement(Element el, String tagName, int index) {
    NodeList list = el.getElementsByTagName(tagName);
    return (Element) list.item(index);
}

From source file:Main.java

/**
 * Gets XML elements//  w w  w.  ja va 2s  .com
 * 
 * @param elementTag
 * @param fromElement
 * @return
 */
public static List getElements(String elementTag, Element fromElement) {
    List elements = new ArrayList();
    NodeList nodeList = fromElement.getElementsByTagName(elementTag);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            elements.add((Element) node);
        }
    }
    return elements;
}

From source file:Main.java

/**
 * @param elem//from  ww  w . j a  v a2s.  co m
 * @param childTag
 * @return
 */
public static Element getFirstChild(Element elem, String childTag) {
    if (elem.hasChildNodes()) {
        NodeList list = elem.getElementsByTagName(childTag);
        int count = list.getLength();

        for (int i = 0; i < count; i++) {
            Node node = list.item(i);
            if (node.getParentNode() != elem)
                continue;

            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) node;
            }
        }
    }

    return null;
}

From source file:Main.java

public static Element element(Element element, String name) {
    Element childElement = null;/*  w  w  w  .  j a v a  2s .  co m*/
    NodeList nodeList = element.getElementsByTagName(name);
    if (nodeList.getLength() > 0) {
        childElement = (Element) nodeList.item(0);
    }
    return childElement;
}

From source file:Main.java

/**
 * I take a xml element and the tag name, look for the tag and get
 * the text content//  ww  w  .j  a  v a 2s  . c  om
 * i.e for <employee><name>John</name></employee> xml snippet if
 * the Element points to employee node and tagName is 'name' I will return John
 */
public static String getTextValue(Element ele, String tagName) {

    // Look for elements first
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        return el.getFirstChild().getNodeValue();
    }

    // If there are no elements look for an attribute
    return ele.getAttribute(tagName);
}

From source file:Main.java

public static List<Element> getElements(Element parent, String tagName) {
    List<Element> elements = new ArrayList<Element>();
    NodeList nodes = parent.getElementsByTagName(tagName);
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element) {
            elements.add(Element.class.cast(node));
        }/*from   w  w w  . ja  v a  2 s.c  o  m*/
    }
    return elements;
}