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

/**
 * Returns a list of the direct child {@link Element}s of the given parent.
 * /*from  w w  w  .j av  a2s  .  c o m*/
 * @param parent The parent element.
 */
public static List<Element> getChildElements(Element parent) {
    List<Element> childElements = new LinkedList<Element>();
    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if (childNode instanceof Element)
            childElements.add((Element) childNode);
    }
    return childElements;
}

From source file:Main.java

/** Returns all the direct child elements of the given element. */
public static List<Element> getElements(Element element) {
    List<Element> elements = new ArrayList<>();
    for (Node node : toIterable(element.getChildNodes())) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            elements.add((Element) node);
        }/*from   w  w  w.j  a  v a2s .  co  m*/
    }
    return elements;
}

From source file:Main.java

public static final List<Element> getChildElements(Element element) {
    List<Element> childElements = new ArrayList<Element>();
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node item = childNodes.item(i);
        if (item.getNodeType() == Node.ELEMENT_NODE) {
            childElements.add((Element) item);
        }/*from   ww  w.ja  va  2 s  .  c  om*/
    }
    return childElements;
}

From source file:Main.java

public static Element getFirstChildElementIgnoreCase(Element elm, String name) {
    if (elm == null)
        return null;

    NodeList nl = elm.getChildNodes();
    for (int c = 0; c < nl.getLength(); c++) {
        Node node = nl.item(c);// w ww .j  ava  2 s .  com
        if (node.getNodeType() == Node.ELEMENT_NODE
                && (name == null || node.getNodeName().equalsIgnoreCase(name)))
            return (Element) node;
    }

    return null;
}

From source file:Main.java

public static List<Element> getChildren(Element element, String tagName) {
    List<Element> children = new ArrayList<>();
    NodeList nodes = element.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element) {
            Element child = (Element) node;
            if (tagName != null && child.getTagName().equals(tagName))
                children.add(child);/*from   ww w . j av a 2 s.  c o m*/
        }
    }
    return children;
}

From source file:Main.java

/**
 * Get the first found child element with the provided name that is a direct
 * child the provided element.//from   w  w  w  . j  av a  2 s  . c  om
 * 
 * @param parent
 *            The parent element
 * @param name
 *            The name of the child element to find
 * @return The first found child element, null if no matching children
 */
public static Element getFirstChildElementByName(Element parent, String name) {
    assertNotNull(parent);
    NodeList children = parent.getChildNodes();
    Node node;
    for (int i = 0; i < children.getLength(); i++) {
        node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) {
            return (Element) node;
        }
    }

    return null;
}

From source file:Main.java

private static Element[] getElementsByTag(Element element, String tag) {
    NodeList children = element.getChildNodes();
    int size = children.getLength();
    int elemCount = 0;
    boolean wildCard = tag == null;

    for (int i = 0; i < size; i++) {
        Node child = children.item(i);
        if (isElement(child) && (wildCard || tag.equals(((Element) child).getTagName()))) {
            elemCount++;//from   w  w  w.  j av  a 2  s.  c o  m
        }
    }

    Element elems[] = new Element[elemCount];
    for (int i = 0, j = 0; j < elemCount; i++) {
        Node child = children.item(i);

        if (isElement(child) && (wildCard || tag.equals(((Element) child).getTagName()))) {
            elems[j++] = (Element) child;
        }
    }

    return elems;
}

From source file:Main.java

/**
 * Get the trimmed text of a text (simple content) element.
 * //from  w  w  w.  j av a 2 s . c om
 * @param e The element.
 * @return The text of the specified element, null if it has no text nodes.
 */
public static String getText(Element e) {
    if (e == null)
        return null;
    NodeList lst = e.getChildNodes();
    int size = lst.getLength();
    for (int i = 0; i < size; i++) {
        Node n = lst.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            Text t = (Text) n;
            String s = t.getData();
            return s == null ? null : s.trim();
        }
    }
    return null;
}

From source file:Main.java

public static List<Element> getChildElements(String tagName, Element element) {
    LinkedList<Element> result = new LinkedList<>();
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);//  ww w.  j  a v  a 2 s . c  o m
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) {
            result.add((Element) n);
        }
    }
    return result;
}

From source file:Main.java

public static Element[] getChildren(Element parent, String name) {
    ArrayList<Element> al = new ArrayList<Element>();
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);//from   w  w w.  j a  v  a2 s  .  c o  m
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            al.add((Element) n);
        }
    }
    return al.toArray(new Element[al.size()]);
}