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

/**
 * Removes any child elements that match the type
 * @param parent The element whose children will be removed
 * @param type The name of the children elements to remove
 *//*  www  . j a  v a  2s.co  m*/
public static void removeChildrenOfType(final Node parent, final String type) {
    final NodeList children = parent.getChildNodes();
    for (int childIndex = 0; childIndex < children.getLength(); ++childIndex) {
        final Node child = children.item(childIndex);
        if (child.getNodeName().equals(type)) {
            parent.removeChild(child);
            break;
        }
    }
}

From source file:Main.java

public static Element findElementWithAttribute(Node node, String tagName, String attrName) {
    Element result = null;/*  w  w  w.  ja v a  2s  . com*/
    NodeList nodeList = node.getChildNodes();
    if (nodeList == null) {
        return result;
    }
    for (int i = 0; i < nodeList.getLength(); ++i) {
        Element element = checkIfElement(nodeList.item(i), tagName);
        if (element != null && element.hasAttribute(attrName)) {
            result = element;
            break;
        }
    }
    return result;
}

From source file:Main.java

/**
 * return the text content of an element
 *//*from w  w w  .j  a  v a2  s  .  c o  m*/
public static String getTextContent(org.w3c.dom.Node element) {
    StringBuffer childtext = new StringBuffer();
    NodeList childlist = element.getChildNodes();
    int ct = childlist.getLength();

    for (int j = 0; j < ct; j++) {
        org.w3c.dom.Node childNode = childlist.item(j);

        if ((childNode.getNodeType() == Node.TEXT_NODE)
                || (childNode.getNodeType() == Node.CDATA_SECTION_NODE)) {
            childtext.append(childNode.getNodeValue().trim());
        }
    }

    return childtext.toString();
}

From source file:Main.java

/**
 * Method allNodesByName./*  w  w  w.j  a  v a2  s  .c  o m*/
 *
 * @param node
 * @param nodeName
 * @return ArrayList
 *         <p/>
 *         Purpose: given a node & tag name, returns an ArrayList of ALL nodes that
 *         have that tag. Useful when there are multiple nodes of the same. For
 *         example, used to parse an entire XML document into an array of testcases.
 */
public static ArrayList allNodesByName(Node node, String nodeName) {
    ArrayList nodes = new ArrayList();
    NodeList currNodes = node.getChildNodes();
    int length = currNodes.getLength();
    for (int i = 0; i < length; i++) {
        Node thisNode = currNodes.item(i);
        if (thisNode.getNodeName().equals(nodeName)) {
            nodes.add(thisNode);
        }
    }
    if (nodes.size() > 0) {
        return nodes;
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * This method creates a map of all of the childern of a node
 * @param root - Parent node//from w w  w . j  a v a2 s.c o  m
 * @return HashMap key value map of the children of the parent node
 */
public static HashMap getMap(Node root) {
    HashMap map = new HashMap();
    if (root != null) {
        NodeList children = root.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            String nodeName = child.getNodeName();
            if (!nodeName.equalsIgnoreCase("#comment") && !nodeName.equalsIgnoreCase("#text")) {
                if (child.getChildNodes().getLength() > 1) {
                    map.put(nodeName, child);
                } else {
                    Node textChild = child.getFirstChild();
                    if (textChild == null) {
                        map.put(nodeName, null);
                    } else {
                        map.put(nodeName, child.getFirstChild().getNodeValue());
                    }
                }
            }
        }
    }
    return map;
}

From source file:Main.java

/**
 * Searches the node for content of type Long. If non-long content is found,
 * it logs a warning and returns null.//from  w w w .j  a  v a 2s .  c  om
 */
public static String[] extractStringArrayFromElement(Node element) {
    // Get all the children
    NodeList children = element.getChildNodes();
    List<String> output = new ArrayList<String>();

    for (int n = 0; n < children.getLength(); n++) {
        Node child = children.item(n);

        if (child.getNodeType() == Node.ELEMENT_NODE) {
            output.add(extractStringFromElement(child));
        }
    }

    return output.toArray(new String[output.size()]);
}

From source file:Main.java

public static Node getChildNode(Node node, String childTag) {
    if (node == null)
        return null;
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        if (childNode != null && childNode.getLocalName() != null
                && childNode.getLocalName().equals(childTag)) {
            return childNode;
        }//ww  w. j ava  2 s. c  om
    }
    return null;
}

From source file:Main.java

public static Node findChildWithTagName(Node parent, String tagName) {
    if (parent == null) {
        return null;
    }/*from  ww  w.j a va  2  s. c  o m*/

    NodeList childs = parent.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        Node child = childs.item(i);
        if (child.getNodeName().equals(tagName)) {
            return child;
        } else if (child.hasChildNodes()) {
            Node result = findChildWithTagName(child, tagName);
            if (result != null) {
                return result;
            }
        }
    }

    return null;
}

From source file:Main.java

public static String getDeepNodeValue(Node node) {
    StringJoiner out = new StringJoiner(" ");

    if (node.hasChildNodes()) {
        for (int i = 0; i < node.getChildNodes().getLength(); i++) {
            String val = getDeepNodeValue(node.getChildNodes().item(i));
            if (val != null && val.trim().length() != 0)
                out.add(val.trim());
        }//  w  ww.  ja va2  s .  c  o  m
    } else if (node.getNodeValue() != null && node.getNodeValue().trim().length() != 0) {
        out.add(node.getNodeValue().trim());
    }

    return out.toString();
}

From source file:Main.java

public static List<Node> getChildElementsByTagName(Node element, String tagName) {
    List<Node> list = new ArrayList<Node>();
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equals(tagName)) {
            list.add(child);/*  www  .  j a va 2  s . c o m*/
        }
    }
    return list;
}