Java XML Node Path getMatchingNodes(Node node, String[] nodePath, int cur, List res)

Here you can find the source of getMatchingNodes(Node node, String[] nodePath, int cur, List res)

Description

get Matching Nodes

License

Open Source License

Declaration

private static void getMatchingNodes(Node node, String[] nodePath,
            int cur, List<Node> res) 

Method Source Code

//package com.java2s;
import org.w3c.dom.*;

import java.util.List;
import java.util.regex.Pattern;

public class Main {
    private static void getMatchingNodes(Node node, Pattern[] nodePath,
            int cur, List<Node> res) {
        if (cur < 0 || cur >= nodePath.length)
            return;
        boolean last = (cur == nodePath.length - 1);
        Pattern pattern = nodePath[cur];
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node c = children.item(i);
            if (pattern.matcher(c.getNodeName()).matches()) {
                if (last) {
                    res.add(c);/*from w ww.j a v  a  2  s . c o m*/
                } else {
                    getMatchingNodes(c, nodePath, cur + 1, res);
                }
            }
        }
    }

    private static void getMatchingNodes(Node node, String[] nodePath,
            int cur, List<Node> res) {
        if (cur < 0 || cur >= nodePath.length)
            return;
        boolean last = (cur == nodePath.length - 1);
        String name = nodePath[cur];
        if (node.hasChildNodes()) {
            NodeList children = node.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                Node c = children.item(i);
                if (name.equals(c.getNodeName())) {
                    if (last) {
                        res.add(c);
                    } else {
                        getMatchingNodes(c, nodePath, cur + 1, res);
                    }
                }
            }
        }
    }
}

Related

  1. getContent(Node n, String path)
  2. getDescendant(Node node, String path)
  3. getElementViaPath(Node node, String path)
  4. getFullPath(Node node)
  5. getIndividualPath(Node individual)
  6. getNode(Node node, String... nodePath)
  7. getNode(Node root, String nodePath)
  8. getNodeCompletePath(Node node)
  9. getNodePath(Node node)