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 only the valid child nodes of a node. Eliminates the nodes from
 * the list of child nodes of the input node if the child node type is same
 * with any of <code>ignoreNodeTypes</code>
 * //  w ww .j a  v  a  2s  . c o m
 * @param node
 * @param maintainOrder
 * @param ignoreNodeTypes
 * @return HashSet<Node>
 */
public static HashSet<Node> getValidChildNodes(Node node, boolean maintainOrder, short... ignoreNodeTypes) {
    NodeList childNodes = node.getChildNodes();
    HashSet<Node> filteredChildNodes = new HashSet<Node>();
    if (maintainOrder) {
        filteredChildNodes = new LinkedHashSet<Node>();
    }
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node thisNode = childNodes.item(i);
        boolean allowAdd = true;
        for (int j = 0; j < ignoreNodeTypes.length; j++) {
            if (ignoreNodeTypes[j] == thisNode.getNodeType()) {
                allowAdd = false;
                break;
            }
        }
        if (allowAdd) {
            filteredChildNodes.add(thisNode);
        }
    }
    return filteredChildNodes;
}

From source file:Main.java

/**
 * This is a helper function to deal with problems that occur when importing Nodes from
 * JTidy Documents to Xerces Documents.//from   w  w  w. j  a v a 2s  . co  m
 */
public static Node importNode(Document d, Node n, boolean deep) {
    Node r = cloneNode(d, n);
    if (deep) {
        NodeList nl = n.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node n1 = importNode(d, nl.item(i), deep);
            r.appendChild(n1);
        }
    }
    return r;
}

From source file:Main.java

public static String getArg(final Node node, final String strName, final String strDefaultValue) {
    return getArg(node.getChildNodes(), strName, strDefaultValue);
}

From source file:Main.java

public static String getNodeValue(Node node) {
    if (node == null) {
        return null;
    }//from  w w  w  .  j  a v a 2s  . c  o m

    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();
        }
    }
    return null;
}

From source file:Main.java

public static Object transformXmlNodesIntoMap(NodeList nodes) {
    Map<String, Object> nodeMap = new HashMap<String, Object>();

    if (nodes.getLength() == 1 && nodes.item(0).getNodeType() == Node.TEXT_NODE)
        // return nodes.item(0).getTextContent();
        return nodes.item(0).getNodeValue();

    for (int nodeIdx = 0; nodeIdx < nodes.getLength(); nodeIdx++) {
        Node node = nodes.item(nodeIdx);
        NodeList subNodes = node.getChildNodes();

        if (node.getNodeType() != Node.TEXT_NODE)
            nodeMap.put(node.getNodeName(), transformXmlNodesIntoMap(subNodes));
    }/* w ww  . jav  a  2 s.  c o m*/
    return nodeMap;
}

From source file:Main.java

public static Node getChildNodeByName(Node node, String name) {
    if (node == null)
        return null;

    NodeList childs = node.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        Node child = childs.item(i);
        if (child.getNodeName().equalsIgnoreCase(name)) {
            return child;
        }//from ww  w .java  2  s.  com

    }
    // not found
    return null;
}

From source file:Main.java

/**
 * Creates the element mapping for the specified nodes and all their child
 * nodes./*from w ww .  ja  v  a  2  s. c  o m*/
 *
 * @param n1 node 1
 * @param n2 node 2
 * @param mapping the mapping to be filled
 */
private static void createElementMappingForNodes(Node n1, Node n2, Map<Node, Node> mapping) {
    mapping.put(n1, n2);
    NodeList childNodes1 = n1.getChildNodes();
    NodeList childNodes2 = n2.getChildNodes();
    int count = Math.min(childNodes1.getLength(), childNodes2.getLength());
    for (int i = 0; i < count; i++) {
        createElementMappingForNodes(childNodes1.item(i), childNodes2.item(i), mapping);
    }
}

From source file:Main.java

/**
 * Finds all children of a node with the given name
 * @param parent the parent node/* w  w  w.  j a va  2 s  .com*/
 * @param name the name
 * @return the child nodes
 */
public static List<Node> findAllChildren(Node parent, String name) {
    List<Node> found = new ArrayList<Node>();
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node node = children.item(i);
        if (name.equals(node.getNodeName())) {
            found.add(node);
        }
    }
    return found;
}

From source file:Main.java

/**
 * Find all child node(immediate children only) under current node that is an element node and node name is tagName
 * This is  a combination of getChildNodes() and getElementByTagName().
 * Also this helper method returns an iterable node list for convinient use in the foreach statement.
 * Only element node directly under currentNode are returned.(i.e no grandchildren).   
 * The order of the children are maintained (removing non-element node) 
 * @param currentNode//  www. java2 s .co m
 * @param tagName - case sensitive
 * @return list of element nodes equals tagname (case sensitive) 
 */
public static List<Node> getChildElementsByTagName(Node currentNode, String tagName) {
    List<Node> results = new ArrayList<Node>();
    NodeList childNodes = currentNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if ((child.getNodeType() == Node.ELEMENT_NODE) && child.getNodeName().equals(tagName))
            results.add(child);
    }
    return results;
}

From source file:Main.java

public static ArrayList<Element> getElementsByTagName(Node node, String tagName) {
    ArrayList<Element> elements = new ArrayList<Element>();
    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        Node n = node.getChildNodes().item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) {
            elements.add((Element) n);
        }/*from ww  w . j a v a  2 s  .c  o m*/
    }
    return elements;
}