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<String> getGrandSonListValueByTagName(Element element, String parentName, String eleName) {

    NodeList nl = element.getElementsByTagName(parentName);
    if (null == nl) {
        return null;
    }//from  w w w  . j a  v a 2s.  c o  m
    Node item = nl.item(0);
    if (null == item) {
        return null;
    }
    NodeList subNodeList = item.getChildNodes();
    List<String> childEles = new ArrayList<String>();
    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(getTextValue((Element) node));
            }
        }
    }

    return childEles;
}

From source file:Main.java

/**
 * Determines if the specified Parent contains the specified Element
 * //from w  ww.  ja  v a  2s .co m
 * @param parent
 * @param elementName
 * @return true: if the Parent contains the Element, false: otherwise
 */
public static boolean contains(Element parent, String elementName) {
    boolean contains = false;

    NodeList nodes = parent.getElementsByTagName(elementName);
    if (nodes != null && nodes.getLength() > 0) {
        contains = true;
    }

    return contains;
}

From source file:Main.java

public static Element getChildElement(Element parent, String tagName, boolean required) {
    NodeList nodes = parent.getElementsByTagName(tagName);
    if (nodes == null || nodes.getLength() == 0) {
        if (required) {
            throw new IllegalStateException(String.format("Missing tag %s in element %s.", tagName, parent));
        } else {/*from   w  w  w. ja  v  a 2  s .  c o  m*/
            return null;
        }
    }
    return (Element) nodes.item(0);
}

From source file:Main.java

/**
 * Retrieve the value of an XML tag. For internal use only during parsing the accounts
 * configuration file./*from ww  w.  j a v  a  2 s . c  o m*/
 *
 * @param element The elemement containing the tag
 * @param tagName The string name of the tag
 * @return Tag value
 */
public static String getTagValue(Element element, String tagName) throws Exception {
    try {
        NodeList tagList = element.getElementsByTagName(tagName);
        Element tagElement = (Element) tagList.item(0);
        NodeList textTagList = tagElement.getChildNodes();

        return (textTagList.item(0)).getNodeValue().trim();
    } catch (Exception e) {
        throw new Exception(
                "Error in parsing the element \"" + element.toString() + "\" with the tag \"" + tagName + "\"");
    }
}

From source file:Main.java

public static String getTagValue(String sTag, Element eElement) {
    if (eElement == null) {
        return null;
    }// w ww  . j av a  2s .  co  m
    NodeList aNodeList = eElement.getElementsByTagName(sTag);
    if (aNodeList == null) {
        return null;
    }
    Node aNode = aNodeList.item(0);
    if (aNode == null) {
        return null;
    }
    NodeList nlList = aNode.getChildNodes();
    Node nValue = (Node) nlList.item(0);
    if (nValue == null) {
        return null;
    }
    return nValue.getNodeValue();
}

From source file:Utils.java

public static Element findElementElseCreateAndSet(Document document, Element parent, String child,
        String value) {//from   w  ww .j a  v  a2  s  .  co  m
    Element ret = null;
    NodeList nl = parent.getElementsByTagName(child);
    if (nl.getLength() == 0) {
        parent.appendChild(document.createElement(child));
        ret = (Element) parent.getElementsByTagName(child).item(0);
        ret.appendChild(document.createTextNode(value));
    }
    return ret;
}

From source file:Main.java

public static String getNamedElemValue(Element parent, String elementName) {
    String val = null;
    NodeList list = parent.getElementsByTagName(elementName);
    if (list.getLength() > 0) {
        val = ((Element) list.item(0)).getFirstChild().getNodeValue();
    }/*from   w ww  .jav  a  2  s .  c o m*/
    return val;
}

From source file:Main.java

static String getChildNodeValue(Element root, String childNodeName, String defaultValue) throws SAXException {
    NodeList nl = root.getElementsByTagName(childNodeName);
    if (nl.getLength() == 1) {
        return getTextValue(nl.item(0)).trim();
    } else if (defaultValue == null) {
        throw new SAXException("Node <" + root.getNodeName() + "> has no child named <" + childNodeName + ">");
    } else {//from   ww w . ja  va  2  s .com
        return defaultValue;
    }
}

From source file:Main.java

public static String[] getNodesValue(Element node, String nodeName) {
    List<String> ret = new ArrayList<String>();

    NodeList ndLs = node.getElementsByTagName(nodeName);
    for (int s = 0; s < ndLs.getLength(); s++) {
        Node fstNode = ndLs.item(s);
        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
            ret.add(fstNode.getTextContent());
        }//from ww  w .  java  2 s.  com
    }

    return ret.toArray(new String[0]);
}

From source file:Main.java

/**
 * Return an iterator for the subelements.
 * //from   w  w w .j a v a2 s .  c  o m
 * @return java.util.Iterator
 * @param element
 *            org.w3c.dom.Element
 * @param name
 *            java.lang.String
 */
public static Iterator<Node> getNodeIterator(Element element, String name) {
    List<Node> list = new ArrayList<Node>();
    NodeList nodeList = element.getElementsByTagName(name);

    int length = nodeList.getLength();
    for (int i = 0; i < length; i++)
        list.add(nodeList.item(i));

    return list.iterator();
}