Example usage for org.w3c.dom Node getChildNodes

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

Introduction

In this page you can find the example usage for org.w3c.dom Node 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 node's first child node that is an element.
 * @param parent/*from   w w  w.java 2s.  c  o  m*/
 * @return first child element, or null
 */
public static Element getFirstChildElement(Node parent) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node n = children.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) n;
        }
    }
    return null;
}

From source file:Main.java

public static Node getFirstValidNode(Node node) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (isValidNode(children.item(i)))
            return children.item(i);
    }/*from w w  w. j  ava2s  .  co  m*/

    return null;
}

From source file:Main.java

public static void removeChildren(Node node) {
    NodeList children = node.getChildNodes();
    if (children != null) {
        int len = children.getLength();
        for (int i = len - 1; i >= 0; i--) {
            node.removeChild(children.item(i));
        }/*from w w  w .java  2 s. c o  m*/
    }
}

From source file:Main.java

public static Node getFirstChildNodeNamed(Node node, String name) {
    NodeList nodes = node.getChildNodes();
    for (int x = 0; x < nodes.getLength(); x++) {
        if (nodes.item(x).getNodeName().equalsIgnoreCase(name) == true) {
            return nodes.item(x);
        }/*from   www. j ava2s.  c o  m*/
    }
    return null;
}

From source file:Main.java

public static String get_inner_text(Node node) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE)
            return child.getNodeValue();
        if (child instanceof Text)
            return ((Text) child).getData();
        /*if (child instanceof TextNode)
           return ((TextNode)child).getData();*/
    }//ww  w. j a va 2  s .c o m
    return null;
}

From source file:Main.java

public static List<Element> childs(Node node) {
    NodeList children = node.getChildNodes();
    List<Element> result = new LinkedList<Element>();
    for (int i = 0; i < children.getLength(); i++) {
        Node c = children.item(i);
        if (c.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }/*from ww  w.  j  a v a  2 s  . c o m*/
        result.add((Element) c);
    }
    return result;
}

From source file:Main.java

/**
 * Preskoci uvodni komentare apod.//from  w w  w .j  a v  a  2s.c om
 * 
 * @param node
 * @return prvni subelement (jediny - korenovy - element v dokumentu)
 */
public static Element getFirstElement(Node node) {
    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if (childNode instanceof Element) {
            return (Element) childNode;
        }
    }
    return null;
}

From source file:Main.java

public static Stream<Node> getChildren(Node node) {
    NodeList children = node.getChildNodes();
    ArrayList<Node> childNodeList = new ArrayList<>();
    for (int c = 0; c < children.getLength(); c++) {
        childNodeList.add(children.item(c));
    }//from  w ww .  j  a  v  a  2  s. c  o m
    Stream<Node> childrenStream = childNodeList.stream();
    return childrenStream;
}

From source file:Main.java

/**
 * Given an node, returns the child text node of this element.
 * /*from  ww w.  j a va  2 s.  c  o m*/
 * @param node
 *            the node to get the text node from
 * @return the text node that is a child of this node, or <CODE>null</CODE>
 *         if there is no such child
 */
public static String containedText(Node node) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node c = children.item(i);
        if (c.getNodeType() != Node.TEXT_NODE)
            continue;
        return ((Text) c).getData();
    }
    return null;
}

From source file:Main.java

private static List<Node> getNonEmptyChildNodes(Node parent) {
    NodeList nl = parent.getChildNodes();
    List<Node> ret = new ArrayList<Node>();
    for (int i = 0; i < nl.getLength(); i++) {
        if (!(nl.item(i) instanceof Text && ((Text) nl.item(i)).getTextContent().trim().length() == 0))
            ret.add(nl.item(i));/*  w  ww  .j av  a 2s. c  o m*/
    }
    return ret;
}