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 List<Node> getChildrenByTagName(Node parent, String tagName) {
    List<Node> eleList = new ArrayList<Node>();
    NodeList nodeList = parent.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equalsIgnoreCase(tagName)) {
            eleList.add(node);//w  w  w. j  a  v  a 2  s.  c  om
        }

    }
    return eleList;
}

From source file:Main.java

public static List<Node> getTextAndElementChildren(Node node) {
    List<Node> result = new LinkedList<Node>();
    NodeList children = node.getChildNodes();
    if (children == null) {
        return result;
    }/*from   ww w .ja v  a 2 s  .  c om*/
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (Node.ELEMENT_NODE == child.getNodeType() || Node.TEXT_NODE == child.getNodeType()) {
            result.add(child);
        }
    }
    return result;
}

From source file:Main.java

public static String getSubTagAttribute(Element root, String tagName, String subTagName, String attribute) {
    String returnString = "";
    NodeList list = root.getElementsByTagName(tagName);
    for (int loop = 0; loop < list.getLength(); loop++) {
        Node node = list.item(loop);
        if (node != null) {
            NodeList children = node.getChildNodes();
            for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) {
                Node child = children.item(innerLoop);
                if ((child != null) && (child.getNodeName() != null)
                        && child.getNodeName().equals(subTagName)) {
                    if (child instanceof Element) {
                        return ((Element) child).getAttribute(attribute);
                    }//from   w w  w  .  j  a  v a 2s. c om
                }
            } // end inner loop
        }
    }
    return returnString;
}

From source file:Main.java

protected static String encodeConstraintsNode(Node constraintsNode) {
    StringBuffer encoded = new StringBuffer();
    NodeList constraints = constraintsNode.getChildNodes();

    for (int i = 0; i < constraints.getLength(); i++) {
        encoded.append(constraints.item(i));
    }/*from  w ww.java  2 s . c o  m*/

    return encoded.toString();
}

From source file:Main.java

private static HashMap<String, String> getValoresNodo(Node nodo) {
    HashMap<String, String> valores = new HashMap<String, String>();
    NodeList listaNodo = nodo.getChildNodes();
    for (int i = 0; i < listaNodo.getLength(); i++) {
        Node n = listaNodo.item(i);
        valores.put(n.getNodeName(), getValorNodo(n));
    }/*from ww w .j  a  va  2 s.co  m*/
    return valores;
}

From source file:Main.java

public static Node[] findChildren(Node node, String name) {
    List<Node> list = new ArrayList<Node>();
    NodeList childNodes = node.getChildNodes();
    int count = childNodes.getLength();
    for (int i = 0; i < count; i++) {
        Node child = childNodes.item(i);
        if (child instanceof Element && (name == null || child.getNodeName().equals(name))) {
            list.add(child);//from www.ja va2  s.  c  om
        }
    }
    return list.toArray(new Node[list.size()]);
}

From source file:Main.java

public static String getValue(final Node node, final String strAttrName, final String strAttrValue) {
    return getValue(node.getChildNodes(), strAttrName, strAttrValue);
}

From source file:Main.java

/**
 * Returns the child of the given node which has the specified name
 * If no such node exists, returns null/*from w ww  . j  a v  a2s.  c o  m*/
 */
public static Node findChild(Node parent, String name) {
    // Iterate through the collection of children
    NodeList nodes = parent.getChildNodes();
    for (int a = 0; a != nodes.getLength(); ++a) {
        Node node = nodes.item(a);
        if (node.getNodeName().equals(name)) {
            // This is the one
            return node;
        }
    }

    // No such node
    return null;
}

From source file:Main.java

public static List<Element> getMultipleElementsByName(Node node, String name) {
    List<Element> elements = new ArrayList<Element>();
    NodeList nodeList = node.getChildNodes();
    int count = nodeList.getLength();
    for (int i = 0; i < count; i++) {
        Node child = nodeList.item(i);
        if (child instanceof Element && child.getNodeName().equals(name)) {
            elements.add((Element) child);
        }/* w ww.jav a2s .c  om*/
    }
    return elements;
}

From source file:Main.java

public static Map<String, String> getChildElements(Node parent) {
    Map<String, String> elements = new HashMap<String, String>();

    NodeList children = parent.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        elements.put(child.getNodeName(), child.getTextContent());
    }/*  w  w w  .j  av  a2s .c  o  m*/

    return elements;
}