Example usage for org.w3c.dom NodeList getLength

List of usage examples for org.w3c.dom NodeList getLength

Introduction

In this page you can find the example usage for org.w3c.dom NodeList getLength.

Prototype

public int getLength();

Source Link

Document

The number of nodes in the list.

Usage

From source file:Main.java

/**
 * get all child elements matching the given tag 
 * //  w w w  . jav a 2 s .  co  m
 * @param tag
 * @param searchIn
 * @return
 */
public static ArrayList<Element> getChildElementsByTag(String tag, Element searchIn) {
    ArrayList<Element> toReturn = new ArrayList<Element>();
    NodeList list = searchIn.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        if (n instanceof Element && ((Element) n).getTagName().equals(tag)) {
            toReturn.add((Element) n);
        }

    }
    return toReturn;
}

From source file:Main.java

public static Element getChildElement(Element elt, String name) {
    NodeList nodes = elt.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
            if (name.equals(node.getNodeName()))
                return (Element) node;
    }//  w  ww. j av a  2  s .c o  m
    return null;
}

From source file:Main.java

/**
 * Gets the first element with the given tag name in the immediate child elements.
 * /*from  ww w  .j av a2 s . c  o m*/
 * @param element
 * @param tagName
 * @return
 */
public static Element getFirstChildElementByTagName(Node element, String tagName) {
    NodeList list = element.getChildNodes();
    int size = list.getLength();
    if (size > 0) {
        for (int i = 0; i < size; i++) {
            Node node = list.item(i);
            if (node instanceof Element) {
                Element e = (Element) node;
                if (e.getTagName().equals(tagName)) {
                    return e;
                }
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * get single element by tag name/*from w w w  . j a  v a2  s  . c o  m*/
 * @param element
 * @param tag
 * @return
 */
public static List<Element> getElementsByTagName(Element element, String tag) {
    List<Element> elems = new ArrayList<Element>();
    NodeList list = element.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            if (e.getTagName().equals(tag)) {
                elems.add(e);
            }
        }
    }
    return elems;
}

From source file:Main.java

/***
 * Remove all children nodes/*from   ww w. j  a v a 2  s  .  c  om*/
 * 
 * @param node
 *            node to remove children from
 * 
 */
public static void removeAllChilden(final Node node) {
    if (node != null) {
        final NodeList childrens = node.getChildNodes();

        for (int i = 0; i < childrens.getLength(); i++) {
            node.removeChild(childrens.item(i));
        }
    }
}

From source file:Main.java

public static Element getChildElement(String tagName, Element element) {
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);//from  ww  w  .  ja v  a 2  s .com
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) {
            return (Element) n;
        }
    }
    return null;
}

From source file:Main.java

public static Element getSingleOptionalChildElement(Element parent, Enum<?> child) {
    final String elName = getXmlName(child);
    final NodeList list = parent.getElementsByTagName(elName);
    if (list.getLength() == 1) {
        return (Element) list.item(0);
    }//from www  .  j  a  v a 2s .c om
    if (list.getLength() == 0) {
        return null;
    }
    throw new IllegalArgumentException("expected a single " + elName + " child element of XML element "
            + parent.getNodeName() + ", but found " + list.getLength());
}

From source file:Main.java

/**
 * Gets the first child element of a node.
 * @param parent the node//from ww w.ja va 2  s  . c  o  m
 * @return the first child element or null if there are no child elements
 */
private static Element getFirstChildElement(Node parent) {
    NodeList nodeList = parent.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node instanceof Element) {
            return (Element) node;
        }
    }
    return null;
}

From source file:Main.java

public static List getChildren(Element parentEl, String name) {
    List children = new ArrayList();
    NodeList l = parentEl.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);/*w ww.  j a  v  a2  s  . com*/
        if (Node.ELEMENT_NODE == n.getNodeType() && name.equals(n.getNodeName())) {
            children.add((Element) n);
        }
    }
    return children;
}

From source file:Main.java

private static boolean hasElementWithAttribute(NodeList nodeList, String attributeName, String attributeValue) {
    for (int i = 0; i < nodeList.getLength(); i++) {
        if (attributeValue.equals(((Element) nodeList.item(i)).getAttribute(attributeName))) {
            return true;
        }/*from   w  w  w  .  j ava  2  s  . com*/
    }
    return false;
}