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

public static Vector findChildren(Node node, String elTag) {
    NodeList children = node.getChildNodes();
    Vector items = new Vector();
    for (int i = 0; i < children.getLength(); i++) {
        Node item = children.item(i);
        if (item.getNodeName().equals(elTag)) {
            items.add(item);/*from  w  ww.ja v  a  2  s.  c  om*/
        }
    }
    return items;
}

From source file:Main.java

/**
 * Returns an array of all the Element children of a DOM node.
 *
 * @param  parent  parent node/*from ww w .java 2  s . c o  m*/
 * @return  children array
 */
public static Element[] getChildren(Node parent) {
    NodeList nodeList = parent.getChildNodes();
    int nnode = nodeList.getLength();
    List elList = new ArrayList(nnode);
    for (int i = 0; i < nnode; i++) {
        Node node = nodeList.item(i);
        if (node instanceof Element) {
            elList.add((Element) node);
        }
    }
    return (Element[]) elList.toArray(new Element[0]);
}

From source file:Main.java

public static void removeAllSubNodesExceptAttributes(Node n) {
    NodeList childNodes = n.getChildNodes();
    List<Node> childNodesToRemove = new ArrayList<Node>();

    for (int i = 0; i < childNodes.getLength(); i++) {
        Node c = childNodes.item(i);

        if (c.getNodeType() != Node.ATTRIBUTE_NODE) {
            childNodesToRemove.add(c);//  ww w.  j  av a 2  s. c o  m
        }
    }

    for (Node c : childNodesToRemove) {
        n.removeChild(c);
    }
}

From source file:Main.java

public static Node findFirstChild(final Node n, final String name) {
    for (int i = 0; i < n.getChildNodes().getLength(); i++) {
        if (n.getChildNodes().item(i).getNodeName().equals(name)) {
            return n.getChildNodes().item(i);
        }/*from   ww w .j a v a 2s  . c  om*/
    }
    throw new IllegalStateException("no " + name + " children found in: " + n.getNodeName());
}

From source file:Main.java

public static Node getChildByName(Node node, String name) {
    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeName().equals(name))
            return nl.item(i);
    }//from   ww  w. j ava  2 s .  c om
    return null;
}

From source file:Main.java

/**
 * @param node//from  w w  w  .j  a  v  a 2  s  .  c  om
 * @param name
 * @return the first child node with the given name or <code>null</code>
 * if none found
 */
public static Node findChild(Node node, String name) {
    NodeList nl = node.getChildNodes();
    int len = nl.getLength();
    Node child;
    for (int i = 0; i < len; i++) {
        child = nl.item(i);
        if (name.equals(child.getNodeName())) {
            return child;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get the value of a node.//w ww . j  a  v  a2 s . co m
 * 
 * @param node node
 * @return string
 */
public static String getValue(Node node) {
    NodeList children = node.getChildNodes();
    if (children.getLength() == 0) {
        return "";
    }
    return children.item(0).getNodeValue();
}

From source file:Main.java

/**
 * get child node by name/*from  www.ja v  a 2  s  .c o m*/
 * 
 * @param parent
 * @param name
 * @return
 */
public static Node getNodeByName(Node parent, String name) {
    for (int i = 0; i < parent.getChildNodes().getLength(); i++) {
        if (parent.getChildNodes().item(i).getNodeName().equals(name))
            return parent.getChildNodes().item(i);
    }

    return null;
}

From source file:Main.java

/**
 * Gets the first child element of a node.
 * @param parent the node/*from   w ww. j  a va2  s  .  co 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

/**
 * Gets node index//ww  w.j a v a  2 s . c  o  m
 * @param parentNode note in which the search
 * @param node seeking node
 * @return node index. -1 if node does not exists
 */
public static int getNodeIndex(Node parentNode, Node node) {
    NodeList list = parentNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if (list.item(i).equals(node)) {
            return i;
        }
    }
    return -1;
}