Example usage for org.w3c.dom.traversal TreeWalker nextNode

List of usage examples for org.w3c.dom.traversal TreeWalker nextNode

Introduction

In this page you can find the example usage for org.w3c.dom.traversal TreeWalker nextNode.

Prototype

public Node nextNode();

Source Link

Document

Moves the TreeWalker to the next visible node in document order relative to the current node, and returns the new node.

Usage

From source file:DemoTreeWalker.java

public static void main(String args[]) throws SAXException, IOException {

    DOMParser parser = new DOMParser();
    parser.parse("games.xml");

    Document doc = parser.getDocument();

    DocumentTraversal docTraversal = (DocumentTraversal) doc;

    TreeWalker iter = docTraversal.createTreeWalker(doc.getDocumentElement(), NodeFilter.SHOW_ALL, null, false);
    Node n = null;//from  w ww.ja v  a 2  s . c  om
    while ((n = iter.nextNode()) != null) {
        System.out.println("Node name: " + n.getNodeName());
        System.out.println("Node value: " + n.getNodeValue());
    }

}

From source file:org.apache.any23.extractor.microdata.MicrodataParser.java

/**
 * Returns all the <b>itemprop</b>s for the given <b>itemscope</b> node.
 *
 * @param scopeNode node representing the <b>itemscope</b>
 * @param skipRoot if <code>true</code> the given root <code>node</code>
 *        will be not read as a property, even if it contains the <b>itemprop</b> attribute.
 * @return the list of <b>itemprop</b>s detected within the given <b>itemscope</b>.
 * @throws MicrodataParserException if an error occurs while retrieving an property value.
 *///  w w w .  j a  va 2 s .co m
public List<ItemProp> getItemProps(final Node scopeNode, boolean skipRoot) throws MicrodataParserException {
    final Set<Node> accepted = new LinkedHashSet<>();

    if (!skipRoot) {
        NamedNodeMap attributes = scopeNode.getAttributes();
        if (attributes.getNamedItem(ITEMPROP_ATTRIBUTE) != null) {
            accepted.add(scopeNode);
        }
    }

    // TreeWalker to walk DOM tree starting with the scopeNode. Nodes maybe visited multiple times.
    TreeWalker treeWalker = ((DocumentTraversal) scopeNode.getOwnerDocument()).createTreeWalker(scopeNode,
            NodeFilter.SHOW_ELEMENT, new NodeFilter() {
                @Override
                public short acceptNode(Node node) {
                    if (node.getNodeType() == Node.ELEMENT_NODE) {
                        NamedNodeMap attributes = node.getAttributes();
                        if (attributes.getNamedItem(ITEMPROP_ATTRIBUTE) != null && !scopeNode.equals(node)) {
                            accepted.add(node);
                        }

                        if (attributes.getNamedItem(ITEMSCOPE_ATTRIBUTE) != null) {
                            // Don't visit descendants of nodes that define a new scope
                            return FILTER_REJECT;
                        }
                    }
                    return FILTER_ACCEPT;
                }
            }, false);

    // To populate accepted we only need to walk the tree.
    while (treeWalker.nextNode() != null)
        ;

    final List<ItemProp> result = new ArrayList<>();
    for (Node itemPropNode : accepted) {
        final String itemProp = DomUtils.readAttribute(itemPropNode, ITEMPROP_ATTRIBUTE, null);

        if (StringUtils.isBlank(itemProp)) {
            manageError(new MicrodataParserException("invalid property name '" + itemProp + "'", itemPropNode));
            continue;
        }

        final String[] propertyNames = itemProp.trim().split("\\s+");
        ItemPropValue itemPropValue;
        for (String propertyName : propertyNames) {
            try {
                itemPropValue = getPropertyValue(itemPropNode);
            } catch (MicrodataParserException mpe) {
                manageError(mpe);
                continue;
            }
            result.add(new ItemProp(DomUtils.getXPathForNode(itemPropNode), propertyName, itemPropValue));
        }
    }
    return result;
}