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[] getDirectElements(Element parent, String name) {

    NodeList children = parent.getChildNodes();
    List<Element> result = new LinkedList<>();

    for (int i = 0; i < children.getLength(); i++) {

        Node node = children.item(i);
        if (node.getNodeName().equals(name)) {
            result.add((Element) node);
        }/*w ww .j  a v a  2  s  .c o m*/
    }

    return result.toArray(new Element[0]);
}

From source file:Main.java

/**
 *///w  w  w  . jav  a2  s  .  co  m
public static String getComment(Element searchIn, int commentIndex) {
    NodeList list = searchIn.getChildNodes();
    int counter = 0;
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        if (n.getNodeType() == Node.COMMENT_NODE) {
            if (counter == commentIndex) {
                return n.getTextContent();
            }
            counter++;
        }
    }
    return null;
}

From source file:Main.java

public static String getTextValue(Element e) {
    NodeList childs = e.getChildNodes();
    for (int k = 0; k < childs.getLength(); k++) {
        Node n = childs.item(k);//from   w w  w.  j  a v  a  2 s .  c  om
        if (n.getNodeType() == 3) {
            return n.getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

public static Element element(Element element) {
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node instanceof Element) {
            return (Element) node;
        }//w  w  w  . j a v  a 2  s. c  o m
    }
    return null;
}

From source file:Main.java

public static List<Element> getAllChildren(Element element) {
    NodeList nodes = element.getChildNodes();

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

From source file:Main.java

public static Element getFirstElement(Element element) {
    Element result = null;//from  ww  w  . ja va2 s. co  m
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i) instanceof Element) {
            result = (Element) nl.item(i);
        }
    }
    return result;
}

From source file:Main.java

public static void cleanWhiteSpaceNodes(Element node, boolean deep) {
    NodeList list = node.getChildNodes();
    ArrayList temp = new ArrayList();
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);/*from w  w w. ja  v  a2s.c  om*/
        short type = n.getNodeType();
        if (type == 1) {
            Element e = (Element) n;
            cleanWhiteSpaceNodes(e, deep);
        } else if (type == 3) {
            Text text = (Text) n;
            String val = text.getData();
            if (val.trim().equals(""))
                temp.add(text);
        }
    }

    for (Iterator i = temp.iterator(); i.hasNext(); node.removeChild((Node) i.next()))
        ;
}

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  w w .j  a va2 s . c o m*/
    return null;
}

From source file:Main.java

/**
 * @param el/*from   w w w  .  j  a  v  a2  s  .  c  o m*/
 * @return the first text child of an element, or null  if it has none
 */
public static Text firstTextChild(Element el) {
    Text text = null;
    for (int i = 0; i < el.getChildNodes().getLength(); i++) {
        Node n = el.getChildNodes().item(i);
        if (n instanceof Text)
            text = (Text) n;
    }
    return text;
}

From source file:Main.java

/**
 * Utility method that returns the first child element
 * identified by its getName./*from   www. j a va 2s  . c  o  m*/
 * @param ele the DOM element to analyze
 * @param childName the child element getName to look for
 * @return the <code>org.w3c.dom.Element</code> creating,
 * or <code>null</code> if none found
 */
public static Element getChildElement(Element ele, String childName) {
    NodeList nl = ele.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element && nodeNameEquals(node, childName))
            return (Element) node;
    }
    return null;
}