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 boolean doesElementContainChildren(Element parent, String... childNames) {
    boolean missingChild = false;
    for (String childName : childNames) {
        if (parent.getElementsByTagName(childName).getLength() == 0) {
            missingChild = true;//from w  ww. j  a v a  2 s  .c o  m
        }
    }
    if (missingChild)
        return false;
    else
        return true;
}

From source file:Main.java

public static String getCharacterDataFromElementWithKey(final Element element, final String key) {
    String result = null;//  w  ww . j a  v a  2  s .c  o  m
    if (element != null) {
        final NodeList jobNodeList = element.getElementsByTagName(key);

        final Node node = jobNodeList.item(0);
        if (node != null) {
            final CharacterData characterData = (CharacterData) jobNodeList.item(0).getChildNodes().item(0);
            if (characterData != null) {
                result = characterData.getNodeValue();
            }
        }
    }
    return result;
}

From source file:Main.java

public static List<Element> getChildElements(Element parent, String tagName) {
    if (null == parent)
        return null;
    NodeList nodes = parent.getElementsByTagName(tagName);
    List<Element> elements = new ArrayList<Element>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getParentNode() == parent)
            elements.add((Element) node);
    }/*w  ww  .  j  a va  2  s.  c  om*/

    return elements;
}

From source file:Main.java

/**
 * Method to return a list of named Elements with a specific attribute value.
 * http://www.java2s.com/Code/Java/XML/ReturnalistofnamedElementswithaspecificattributevalue.htm
 * @param element the containing Element.
 * @param name the tag name.//from   w  w w  .  j  a v a 2  s  . c  om
 * @param attribute Attribute name.
 * @param value Attribute value.
 * @param returnFirst Return only the first matching value?.
 * @return List of matching elements.
 */
public static List<Node> selectElementsByAttributeValue(Element element, String name, String attribute,
        String value, boolean returnFirst) {
    NodeList elementList = element.getElementsByTagName(name);
    List<Node> resultList = new ArrayList<>();
    for (int i = 0; i < elementList.getLength(); i++) {
        if (getAttribute((Element) elementList.item(i), attribute).equals(value)) {
            resultList.add(elementList.item(i));
            if (returnFirst) {
                break;
            }
        }
    }
    return resultList;
}

From source file:Main.java

public static String SearchForElementText(Element element, String name) {
    if (element != null && name != null && !name.isEmpty()) {
        NodeList nodes = element.getElementsByTagName(name);

        if (nodes != null && nodes.getLength() > 0) {
            return nodes.item(0).getTextContent();
        }/*from ww w  .j  av a  2  s  .  c  o m*/
    }

    return null;
}

From source file:Main.java

public static Element getElementByTagName(Element current, String name, String namespace) {

    NodeList elements = null;//from  w ww  .j  ava 2 s  .c  o m

    if (namespace == null)
        elements = current.getElementsByTagName(name);
    else
        elements = current.getElementsByTagNameNS(namespace, name);

    if (elements == null)
        return null;

    if (elements.getLength() != 1)
        return null;

    return (Element) elements.item(0);

}

From source file:Main.java

/**
 * Get the specified element from a parent element.
 * /*www  .j  a v  a 2 s  .  c o m*/
 * @param elementName the element tag to serach for.
 * @param parentElement the parent element.
 * @return an element given the name and the parent element.
 */
public static Element getElement(String elementName, Element parentElement) {
    Element returnElement = null;
    NodeList nl;

    // Get the list of elements
    if ((nl = parentElement.getElementsByTagName(elementName)) != null) {
        // Return first element found
        returnElement = (Element) nl.item(0);
    }

    // Return element
    return returnElement;
}

From source file:Main.java

/**
 * Get the first child element with the given name
 *
 * @param parentElement//  www .j av a  2s .  com
 * @param name
 * @return
 */
public static Node getChildElement(Element parentElement, String name, String... filters) {
    final NodeList nodeList = parentElement.getElementsByTagName(name);

    if (nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            final Node node = (Node) nodeList.item(i);

            if (node instanceof Element && matchesFilters((Element) node, filters)) {
                return node;
            } else if (node instanceof Attr) {
                return node;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns the value of given tagName under given element.
 * //www  .  ja  va2 s .  c o  m
 * E.g.
 * <ele>
 *    <tagName>MyValue</tagName>
 * </ele>
 * 
 * MyValue is returned in the above case.
 * 
 * @param ele xml element under which the tagName should be found
 * @param tagName tag name of the tag which value is read
 * @return the value of given tagName under given element.
 */
public static String getTextValue(Element ele, String tagName) {
    String textVal = null;

    try {
        NodeList nl = ele.getElementsByTagName(tagName);
        if (nl != null && nl.getLength() > 0) {
            Element el = (Element) nl.item(0);
            Node firstChild = el.getFirstChild();
            if (firstChild != null)
                textVal = firstChild.getNodeValue();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return textVal;
}

From source file:Main.java

/**
 * This method takes Element object and String tagName as argument and
 * returns the value of the first child node it gets. If it does not find
 * any node it will return null.//from   ww w . j  a v a2  s  .c  o m
 * 
 * @param element
 *            Element object
 * @param tagName
 *            Name of the tag.
 * @return the value of the first occurance of <code>tagName</code> tag in
 *         <code>element</code> object. Returns null, if doesn't find any.
 */
public static String getTagValue(Element element, String tagName) {
    if (element == null) {
        return null;
    }
    NodeList nodes = element.getElementsByTagName(tagName);
    if (nodes == null || nodes.getLength() <= 0) {
        return null;
    }
    return getTagValue(nodes.item(0));
}