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 String getSubTagValue(Element root, String tagName, String subTagName) {
    String returnString = "";
    NodeList list = root.getElementsByTagName(tagName);
    for (int loop = 0; loop < list.getLength(); loop++) {
        Node node = list.item(loop);
        if (node != null) {
            NodeList children = node.getChildNodes();
            for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) {
                Node child = children.item(innerLoop);
                if ((child != null) && (child.getNodeName() != null)
                        && child.getNodeName().equals(subTagName)) {
                    Node grandChild = child.getFirstChild();
                    if (grandChild.getNodeValue() != null)
                        return grandChild.getNodeValue();
                }//from   ww w . java  2  s. co m
            } // end inner loop
        }
    }
    return returnString;
}

From source file:Main.java

public static String getValue(Element e, String tagName) {
    try {/*from  w  w w.  j  a v  a 2 s . c  om*/
        // get node lists of a tag name from a Element
        NodeList elements = e.getElementsByTagName(tagName);

        if (elements.getLength() == 0) {
            return null;
        }
        Node node = elements.item(0);
        return node.getTextContent();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getSubTagAttribute(Element root, String tagName, String subTagName, String attribute) {
    String returnString = "";
    NodeList list = root.getElementsByTagName(tagName);
    for (int loop = 0; loop < list.getLength(); loop++) {
        Node node = list.item(loop);
        if (node != null) {
            NodeList children = node.getChildNodes();
            for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) {
                Node child = children.item(innerLoop);
                if ((child != null) && (child.getNodeName() != null)
                        && child.getNodeName().equals(subTagName)) {
                    if (child instanceof Element) {
                        return ((Element) child).getAttribute(attribute);
                    }//w w w  .java  2  s  .com
                }
            } // end inner loop
        }
    }
    return returnString;
}

From source file:Main.java

public static String getSubTagValue(Element root, String tagName, String subTagName) {
    String returnString = "";
    NodeList list = root.getElementsByTagName(tagName);
    for (int loop = 0; loop < list.getLength(); loop++) {
        Node node = list.item(loop);
        if (node != null) {
            NodeList children = node.getChildNodes();
            for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) {
                Node child = children.item(innerLoop);
                if ((child != null) && (child.getNodeName() != null)
                        && (child.getNodeName().equals(subTagName))) {
                    Node grandChild = child.getFirstChild();
                    if (grandChild.getNodeValue() != null) {
                        return grandChild.getNodeValue();
                    }// w  w  w  .ja va2  s. c om
                }
            }
        }
    }
    return returnString;
}

From source file:Utils.java

public static Element findElementElseCreateAndAttribute(Document document, Element parent, String element,
        String attributeName, String attributeValue) {
    NodeList nl = parent.getElementsByTagName(element);
    Element e = null;// w w w  . java 2 s  . c  o  m

    if (nl.getLength() == 0) {
        parent.appendChild(document.createElement(element));
        e = (Element) parent.getElementsByTagName(element).item(0);
        e.setAttribute(attributeName, attributeValue);
    }

    return e;
}

From source file:Main.java

public static List getChildElements(Element p_rootElement, String p_elementName) {
    List result = new ArrayList();
    NodeList nodeList = p_rootElement.getElementsByTagName(p_elementName);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element elem = (Element) nodeList.item(i);
        result.add(elem);/*from  w w  w . jav a 2 s .c o  m*/
    }
    return result;
}

From source file:Main.java

/**
 * Returns an array of strings representing filtered values for provided
 * attribute prefixed by 2 levels of parents, based on a parent node (e.g.:
 * parenetNodeAtrValue.childNodeAtrValue.childNodeAtrValue)
 * @param parent//from  www .  j  av a  2s  .  co m
 * @param atrName
 * @return
 */
protected static ArrayList<String> getFilteredValuesWithParentForAttribute(Element parent, String atrName) {
    try {
        NodeList childNodes = parent.getElementsByTagName("*");
        return getValuesWithParentForAttribute(atrName, childNodes);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * find element with tagName whose id tag is 'idTagName' and id value is 'id_value'
 *
 * @param root/* w  ww .j a v a2 s  . c  o m*/
 */
public static Element findElement(final String idValue, final String idTagName, final String tagName,
        final Element root) {
    String textVal = null;
    final NodeList nl = root.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        // iterate over these
        for (int i = 0; i < nl.getLength(); i++) {
            final Element ei = (Element) nl.item(i);
            final NodeList n2 = ei.getElementsByTagName(idTagName);
            if (n2 != null && n2.getLength() > 0) {
                for (int j = 0; j < n2.getLength(); j++) {
                    textVal = getElementValue((Element) n2.item(j));
                    if (textVal.equals(idValue)) {
                        return ei;
                    }
                }
            }
        }
    }
    return null;
}

From source file:net.ftb.data.news.RSSReader.java

private static String getTextValue(Element doc, String tag) {
    String value = "";
    NodeList nl;//from w  w w . ja  v  a2  s  .  c o m
    nl = doc.getElementsByTagName(tag);
    if (nl.getLength() > 0 && nl.item(0).hasChildNodes()) {
        value = nl.item(0).getFirstChild().getNodeValue();
    }
    return value;
}

From source file:Main.java

/**
 * Returns a child element with a given name from an XML element
 * //from w w  w.j a  v  a2  s . co m
 * @param element
 *            A Element
 * @param tagName
 *            The name of the child element
 * @return The child element or <code>null</code> if no such child exists
 */
public static Element getChildElement(Element element, String tagName) {
    if (element == null)
        return null;
    NodeList list = element.getElementsByTagName(tagName);
    if (list != null && list.getLength() > 0) {
        return (Element) list.item(0);
    }
    return null;
}