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

/**
 * @param parentNode/*from ww w .  j av  a 2s.  c  om*/
 *            is the element tag that contains all the AttirbuteValuePair
 *            tags as children
 * @return Map Returns the AV pairs in a Map where each entry in the Map is
 *         an AV pair. The key is the attribute name and the value is a Set
 *         of String objects.
 */
public static Map<String, Set<String>> parseAttributeValuePairTags(Node parentNode) {
    NodeList keyValueList = parentNode.getChildNodes();
    int keyValueSize = keyValueList.getLength();

    if (keyValueSize <= 0) {
        return EMPTY_MAP;
    }

    Map<String, Set<String>> resultMap = null;

    for (int l = 0; l < keyValueSize; l++) {
        Node keyValueNode = keyValueList.item(l);

        if (keyValueNode.getNodeType() != Node.ELEMENT_NODE
                || !keyValueNode.getNodeName().equals("AttributeValuePair")) {
            continue;
        }

        NodeList keyValueEntryList = keyValueNode.getChildNodes();
        int keyValueEntrySize = keyValueEntryList.getLength();
        if (keyValueEntrySize < 2) {
            // TODO: More error handling required later for missing
            // 'Attribute' or 'Value' tags.
            continue;
        }

        Node keyNode = null;

        // Since Attribute tag is always the first leaf node as per the DTD, and values can one or more,
        // Attribute tag can be parsed first and then iterate over the values, if any.
        for (int i = 0; i < keyValueEntrySize; i++) {
            keyNode = keyValueEntryList.item(i);

            if (keyNode.getNodeType() == Node.ELEMENT_NODE && keyNode.getNodeName().equals("Attribute")) {
                break;
            }
        }

        final String key = ((Element) keyNode).getAttribute("name");
        Set<String> values = null;

        // Now parse the Value tags. If there are not 'Value' tags, ignore this key
        // TODO: More error handling required later for zero 'Value' tags.
        for (int m = 0; m < keyValueEntrySize; m++) {
            Node valueNode = keyValueEntryList.item(m);

            if (valueNode.getNodeType() != Node.ELEMENT_NODE || !valueNode.getNodeName().equals("Value")) {
                // TODO: Error handling required here
                continue;
            }

            if (values == null) {
                values = new HashSet<String>();
            }

            Node firstChild = valueNode.getFirstChild();
            if (firstChild != null) {
                String value = firstChild.getNodeValue();

                if (value != null) {
                    values.add(value.trim());
                }
            }
        }

        if (values == null) {
            // No 'Value' tags found. So ignore this key.
            // TODO: More error handling required later for zero
            // 'Value' tags.
            continue;
        }

        if (resultMap == null) {
            resultMap = new HashMap<String, Set<String>>();
        }

        Set<String> oldValues = resultMap.get(key);
        if (oldValues != null) {
            values.addAll(oldValues);
        }
        resultMap.put(key, values);
    }

    return resultMap == null ? EMPTY_MAP : resultMap;
}

From source file:Main.java

/**
 * Finds a Node with a given QNameb./*  w  w  w  .  ja  v a 2 s .  co  m*/
 *
 * @param node parent node
 * @param name QName of the child we need to find
 * @return Returns child node.
 */
public static Node findNode(Node node, QName name) {
    if (name.getNamespaceURI().equals(node.getNamespaceURI())
            && name.getLocalPart().equals(node.getLocalName())) {
        return node;
    }
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node ret = findNode(children.item(i), name);
        if (ret != null) {
            return ret;
        }
    }
    return null;
}

From source file:com.iggroup.oss.restdoclet.plugin.util.XmlUtils.java

/**
 * Returns the child-elements of a XML node with a particular name.
 * //  w  w w  .  j  a  v a  2 s . c  o  m
 * @param node the XML node.
 * @param name the name of the child-elements.
 * @return the collection of child-elements or an empty collection if no
 *         children with the name were found.
 */
public static Collection<Element> children(final Node node, final String name) {
    final Collection<Element> elements = new ArrayList<Element>();

    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        if (node.getChildNodes().item(i).getNodeType() == ELEMENT_NODE) {
            final Element child = (Element) node.getChildNodes().item(i);

            if (StringUtils.equals(name, child.getNodeName())) {
                elements.add(child);
            }
        }
    }
    return elements;
}

From source file:Main.java

public static List<Node> getMatchingChildNodes(Node node, String str, String str2, List<String> list) {
    if (node == null || str == null) {
        return null;
    }/*from   w  w  w .  j a v  a 2 s .  c o m*/
    List<Node> arrayList = new ArrayList();
    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node item = childNodes.item(i);
        if (item.getNodeName().equals(str) && nodeMatchesAttributeFilter(item, str2, list)) {
            arrayList.add(item);
        }
    }
    return arrayList;
}

From source file:DocWriter.java

private static void writeNode(Node node, Writer out, String indent) throws IOException {
    out.write(nodeWithAttrs(node, indent));
    NodeList kids = node.getChildNodes();
    if (kids != null) {
        if ((kids.item(0) != null) && (kids.item(0).getNodeType() == Node.ELEMENT_NODE)) {
            out.write('\n');
        }/*from  w  w w .j  a v a  2 s  .  co  m*/
        for (int i = 0; i < kids.getLength(); i++) {
            serializeNode(kids.item(i), out, indent + SINGLE_INDENT);
        }
        /* RLR - bug in indent logic - seems to work OK without
           if ( ( kids.item( 0 ) != null ) &&
         ( kids.item( kids.getLength()-1).getNodeType() == Node.ELEMENT_NODE ) )
           {
        out.write( indent );
           }
           */
    }
    if (node.hasChildNodes()) {
        /* indent bug - leave out
        if ( kids.item( 0 ) != null && kids.item( 0 ).getNodeType() == Node.ELEMENT_NODE )
        {
           out.write( indent );
        }
        */
        out.write("</" + node.getNodeName() + ">");
    }
}

From source file:Main.java

private static void cleanWhiteList(Node node, ArrayList<String> whiteList) {
    if (whiteList.contains(node.getLocalName())) {
        node.setNodeValue(null);//from w  w  w .  j ava  2 s.c  o m
        node.setTextContent(null);
        // System.err.println("haha");
    }
    NodeList children = node.getChildNodes();
    if (children.getLength() != 0) {
        for (int i = 0; i < children.getLength(); i++)
            cleanWhiteList(children.item(i), whiteList);
    }

}

From source file:eu.planets_project.services.utils.cli.CliMigrationPaths.java

private static Set<URI> decodeFromOrToNode(Node urilist) throws URISyntaxException {
    NodeList children = urilist.getChildNodes();
    Set<URI> uris = new HashSet<URI>();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (child.getNodeName().equals("uri")) {
                URI uri = decodeURI(child);
                uris.add(uri);//from w  w w.j a va2  s  . co  m
            }
        }
    }
    return uris;
}

From source file:com.betfair.testing.utils.cougar.assertions.AssertionUtils.java

private static void doDomSorting(Document doc, XPath xpath, String x)
        throws XPathExpressionException, IOException {
    NodeList parentNodes = (NodeList) xpath.evaluate(x, doc, XPathConstants.NODESET);
    for (int i = 0; i < parentNodes.getLength(); i++) {
        Node n = parentNodes.item(i);
        List<Node> allKids = new ArrayList<>(n.getChildNodes().getLength());
        for (int j = n.getChildNodes().getLength() - 1; j >= 0; j--) {
            allKids.add(n.removeChild(n.getFirstChild()));
        }// w  w  w. j ava2s  .  co m
        final Map<Node, String> kidsToString = new HashMap<>();

        for (Node k : allKids) {
            kidsToString.put(k, toString(k));
        }
        Collections.sort(allKids, new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                return kidsToString.get(o1).compareTo(kidsToString.get(o2));
            }
        });
        for (Node k : allKids) {
            n.appendChild(k);
        }
    }
}

From source file:Main.java

/**
 * For the given {@code sourceNode}, read each "top level" element into the resulting {@code Map}. Each element name
 * is a key to the map, each element value is the value paired to the key. Example - anchor is the node, label and
 * href are keys:/*  www.ja  va  2s  .  c o m*/
 *
 * <pre>
 * {@code
 * <anchor>
 *  <label>Slashdot</label>
 *    <href>http://slashdot.org/</href>
 *  </anchor>
 * }
 * </pre>
 */
public static Map<String, String> readNodeElementsToMap(final Node sourceNode) {
    Map<String, String> result = new HashMap<String, String>();

    if (sourceNode == null) {
        return result;
    }

    NodeList childNodes = sourceNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node element = childNodes.item(i);

        if (element.getNodeType() == Node.ELEMENT_NODE) {
            String elementName = element.getNodeName();
            String elementValue = "";
            Node firstChild = element.getFirstChild();

            if (firstChild != null) {
                elementValue = firstChild.getNodeValue();
            }

            if (elementValue != null) {
                result.put(elementName, elementValue);
            }
        }
    }

    return result;
}

From source file:Main.java

public static String nodeToString(Node node, boolean inner) throws TransformerException {
    if (!inner) {
        return nodeToString(node);
    } else {/*  w w  w.  j  a  va  2  s. c o  m*/
        StringBuffer buf = new StringBuffer();
        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            nodeToString(nodeList.item(i), buf);
        }
        return buf.toString();
    }
}