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<Node> getGrandSonElementsByTagName(Element ele, String parentName, String eleName) {

    NodeList nl = ele.getElementsByTagName(parentName);
    if (null == nl) {
        return null;
    }/* www  . j ava 2 s  . c o m*/
    Node item = nl.item(0);
    if (null == item) {
        return null;
    }
    NodeList subNodeList = item.getChildNodes();
    List<Node> childEles = new ArrayList<Node>();
    Node node = null;
    for (int i = 0; i < subNodeList.getLength(); i++) {
        node = subNodeList.item(i);

        if (node != null) {
            if (node instanceof Element && eleName.equals(node.getNodeName())
                    || eleName.equals(node.getLocalName())) {
                childEles.add(node);
            }
        }
    }

    return childEles;
}

From source file:Main.java

public static String getStringNodeValue(Node node, String nodeName) {
    Element fstElmnt = (Element) node;
    NodeList fstNmElmntLst = fstElmnt.getElementsByTagName(nodeName);

    if (fstNmElmntLst.getLength() == 0)
        return "";

    Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
    NodeList fstNm = fstNmElmnt.getChildNodes();

    if (fstNm.getLength() > 0) {
        String retValue = ((Node) fstNm.item(0)).getNodeValue();
        try {//from   w w  w.  j av a2s  . co m
            return new String(retValue.getBytes("ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
        }
    }
    return "";
}

From source file:Main.java

/**
 * Gets an element from the XML/*www  .  j  a va 2  s. com*/
 * 
 * @param elementTag
 * @param fromElement
 * @return
 */
public static Element getElement(String elementTag, Element fromElement) {
    Element element = null;
    NodeList nodeList = fromElement.getElementsByTagName(elementTag);
    if (nodeList.getLength() > 0) {
        Node node = nodeList.item(0);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            element = (Element) node;
        }
    }
    return element;
}

From source file:Main.java

/**
 * Return content of child element with given tag name.
 * If more than one children with this name are present, the content of the last
 * element is returned./*  w w w .j a v  a2  s  .c  om*/
 *
 * @param elem
 * @param childTagName
 * @return content of child element or null if no child element with this name was found
 */
public static String getChildText(Element elem, String childTagName) {
    NodeList nodeList = elem.getElementsByTagName(childTagName);
    int len = nodeList.getLength();
    if (len == 0) {
        return null;
    }

    return getElementText((Element) nodeList.item(len - 1));

}

From source file:Main.java

public static String[] getElementsByTagNameArray(String sTag, Element eElement) {
    String[] array = new String[0];
    NodeList nlList = eElement.getElementsByTagName(sTag);
    if (nlList != null && nlList.item(0) != null) {
        NodeList nodes = nlList.item(0).getChildNodes();
        array = new String[nodes.getLength()];
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            array[i] = node.getTextContent();
        }/*from ww  w .  j  av  a  2  s. co m*/
    }
    return array;
}

From source file:Main.java

/**
 * Get the first instance of an element by name.
 * //  www .j a  va2 s .c  o  m
 * @param parent
 *            The parent to get the element from.
 * @param elementName
 *            The name of the element to look for.
 * @return The element or null if it is not found.
 */
public static Element getElement(Element parent, String elementName) {
    Element retval = null;
    final NodeList children = parent.getElementsByTagName(elementName);
    if (children.getLength() > 0) {
        retval = (Element) children.item(0);
    }
    return retval;
}

From source file:Main.java

public static Vector getElementsByAttribValue(org.w3c.dom.Element element, String attrib, String val) {
    NodeList desElements = element.getElementsByTagName("*");
    Vector selElements = new Vector(desElements.getLength() / 10, 10);

    for (int i = 0; i < desElements.getLength(); i++) {
        org.w3c.dom.Node desElement = desElements.item(i);

        if (desElement.getNodeType() == org.w3c.dom.Element.ELEMENT_NODE) {
            NamedNodeMap attributeNodes = desElement.getAttributes();

            org.w3c.dom.Node selAttribNode = attributeNodes.getNamedItem(attrib);

            if (selAttribNode != null && selAttribNode.getNodeValue().equalsIgnoreCase(val)) {
                selElements.add(desElement);
            }//from w ww  . ja va 2 s.com
        }
    }

    return selElements;
}

From source file:Main.java

public static List elements(Element element, String tagName) {
    NodeList nodeList = element.getElementsByTagName(tagName);
    List elements = new ArrayList(nodeList.getLength());
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node child = nodeList.item(i);
        if (child.getParentNode() == element) {
            elements.add(child);/*from w  ww . ja  v  a 2 s.  c  om*/
        }
    }
    return elements;
}

From source file:Main.java

/**
 * Method t o return a single first Element with a specific attribute value. 
 * (maybe you can find a better method))
 * @param tagName string of the name tag xml.
 * @param nameAttribute string of the name attribute xml.
 * @return the element xml with the specific attribute.
 *//* w  w  w.ja v a  2 s  .  c  om*/
public static Element selectFirstElementByAttribute(String tagName, String nameAttribute) {
    Element el = doc.getDocumentElement(); //get root element
    NodeList elementList = el.getElementsByTagName(tagName);
    for (int i = 0; i < elementList.getLength(); i++) {
        if (getAttribute((Element) elementList.item(i), nameAttribute) != null) {
            el = (Element) elementList.item(i);
            break;
        }
    }
    return el;
}

From source file:Main.java

/**
 * I take a xml element and the tag name, look for the tag and get
 * the text content /*  www.ja  va 2s. c  o  m*/
 * 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  
 * @param ele
 * @param tagName
 * @return
 */
public static String getTextValue(Element ele, String tagName) {
    String textVal = null;
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        textVal = el.getFirstChild().getNodeValue();
        if (textVal != null) {
            textVal = textVal.trim();
        }
    }

    return textVal;
}