Example usage for org.w3c.dom Element getChildNodes

List of usage examples for org.w3c.dom Element getChildNodes

Introduction

In this page you can find the example usage for org.w3c.dom Element getChildNodes.

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:Main.java

public static Element getChild(String name, Element e) {
    for (int i = 0; i < e.getChildNodes().getLength(); i++) {
        if (e.getChildNodes().item(i).getNodeName().equals(name)) {
            return (Element) e.getChildNodes().item(i);
        }/*from w  ww  .j  a  va 2  s. c  o m*/
    }
    return null;
}

From source file:Main.java

public static List<Element> getChildElements(Element element) {
    NodeList childNodes = element.getChildNodes();

    List<Element> elements = new ArrayList<Element>();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node n = childNodes.item(i);
        if (n instanceof Element) {
            elements.add((Element) n);
        }/*from w  ww .ja v a 2  s . c  o  m*/
    }
    return elements;
}

From source file:Main.java

/**
 * /*from   ww w  .j  a v  a2  s.c  om*/
 * @param e
 * @return
 */
public static String getText(Element e) {
    NodeList nl = e.getChildNodes();
    int max = nl.getLength();
    for (int i = 0; i < max; i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            return n.getNodeValue().trim();
        }
    }
    return "";
}

From source file:Main.java

public static List<Element> getChildNodes(Element parent) {
    NodeList list = parent.getChildNodes();
    List<Element> children = new ArrayList<Element>(list.getLength());
    for (int i = 0; i < list.getLength(); i++) {
        Node child = list.item(i);
        if (child.getNodeType() == 1)
            children.add((Element) child);
    }//from   w  ww .j a v  a2s .  c  om

    return children;
}

From source file:Main.java

/**
 * Indicates whether element has any child element.
 *
 * @param element the namespace to analyze.
 * @return true if element has any child element otherwise false.
 *///  www. j a  v a 2 s. c o m
public static boolean hasChildElements(Element element) {
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * Returns the first node that is a direct child of root with the coresponding
 * name.  Does not search children of children.
 *///  w ww  .  j  a  v a2  s .c o m
public static Element getFirstChild(Element root, String name) {
    NodeList nl = root.getChildNodes();
    int size = nl.getLength();
    for (int i = 0; i < size; i++) {
        Node node = nl.item(i);
        if (!(node instanceof Element))
            continue;
        Element ele = (Element) node;
        if (ele.getTagName().equals(name))
            return ele;
    }

    return null;
}

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);//w w  w  .j ava2 s  .co  m
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) {
            return (Element) n;
        }
    }
    return null;
}

From source file:Main.java

/**
 * An iterable for all the Element childs of a node
 *
 * @param n the node get the children from
 * @return An iterable for all the Element childs of a node
 *//*from  w w w  .  j av a2 s  .  c  om*/
public static Iterable<Element> elements(Element n) {
    int sz = n.getChildNodes().getLength();
    ArrayList<Element> elements = new ArrayList<>(sz);
    for (int idx = 0; idx < sz; idx++) {
        Node node = n.getChildNodes().item(idx);
        if (node instanceof Element) {
            elements.add((Element) node);
        }
    }
    return elements;
}

From source file:Main.java

/**
 * get single element by tag name//from  ww w  .j av  a2 s  .  c  om
 * @param element
 * @param tag
 * @return
 */
public static Element getElementByTagName(Element element, String tag) {
    NodeList list = element.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node instanceof Element && ((Element) node).getTagName().equals(tag)) {
            return (Element) node;
        }
    }
    return null;
}

From source file:Main.java

private final static boolean hasChilds(final Element element) {

    final NodeList list = element.getChildNodes();
    final int length = list.getLength();
    for (int i = 0; i < length; i++) {
        final Node node = list.item(i);
        if (node instanceof Element) {
            return true;
        }//from w ww .  j  a va2 s .  co  m
    }

    return false;
}