Java XML Node Path getNode(Node node, String... nodePath)

Here you can find the source of getNode(Node node, String... nodePath)

Description

get Node

License

Open Source License

Declaration

public static Node getNode(Node node, String... nodePath) 

Method Source Code

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

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

public class Main {
    public static Node getNode(Node node, Pattern... nodePath) {
        List<Node> nodes = getNodes(node, nodePath);
        if (nodes != null && nodes.size() > 0) {
            return nodes.get(0);
        } else {//from   w  w w .jav  a  2s  .  c om
            return null;
        }
    }

    public static Node getNode(Node node, String... nodePath) {
        List<Node> nodes = getNodes(node, nodePath);
        if (nodes != null && nodes.size() > 0) {
            return nodes.get(0);
        } else {
            return null;
        }
    }

    public static List<Node> getNodes(Node node, Pattern... nodePath) {
        List<Node> res = new ArrayList<>();
        getMatchingNodes(node, nodePath, 0, res);
        return res;
    }

    public static List<Node> getNodes(Node node, String... nodePath) {
        List<Node> res = new ArrayList<>();
        getMatchingNodes(node, nodePath, 0, res);
        return res;
    }

    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);
                } 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. getDescendant(Node node, String path)
  2. getElementViaPath(Node node, String path)
  3. getFullPath(Node node)
  4. getIndividualPath(Node individual)
  5. getMatchingNodes(Node node, String[] nodePath, int cur, List res)
  6. getNode(Node root, String nodePath)
  7. getNodeCompletePath(Node node)
  8. getNodePath(Node node)
  9. getNodePath(Node node)