Java XPath Select xmlSelectNodes(Node node, String xpathExpression)

Here you can find the source of xmlSelectNodes(Node node, String xpathExpression)

Description

Oh, for a decent xml library ;).

License

Open Source License

Declaration

public static List xmlSelectNodes(Node node, String xpathExpression) 

Method Source Code

//package com.java2s;
/*//from  w  w  w  .j  a  v a2 s  .com
 *   SoftSqueeze Copyright (c) 2004 Richard Titmuss
 *
 *   This file is part of SoftSqueeze.
 *
 *   SoftSqueeze is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   SoftSqueeze is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with SoftSqueeze; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Oh, for a decent xml library ;). This method returns a list of nodes
     * matching very simple xml xpath expressions.
     */
    public static List xmlSelectNodes(Node node, String xpathExpression) {
        List list = new LinkedList();

        String path[] = split(xpathExpression, "/");
        xmlSelectNodes0(list, node, path, 0);

        return list;
    }

    /**
     * Splits a string using the given delimiters. Returns an array of strings.
     * This is equivalent to String.split("\s*[delim]\s*") in jdk1.4.
     */
    public static String[] split(String str, String delim) {
        StringTokenizer tok = new StringTokenizer(str, delim);

        int n = tok.countTokens();
        String split[] = new String[n];

        for (int i = 0; tok.hasMoreTokens(); i++)
            split[i] = tok.nextToken().trim();

        return split;
    }

    private static void xmlSelectNodes0(List list, Node node, String path[], int n) {
        if (n == path.length) {
            list.add(node);
            return;
        }

        if (path[n].equals("..")) {
            xmlSelectNodes0(list, node.getParentNode(), path, n + 1);
            return;
        }

        NodeList kids = node.getChildNodes();
        for (int i = 0; i < kids.getLength(); i++) {
            Node kid = kids.item(i);

            if (kid.getNodeName().equals(path[n]))
                xmlSelectNodes0(list, kid, path, n + 1);
        }
    }
}

Related

  1. selectSingleNode(final Node sourceNode, final String xPathExpression)
  2. selectSingleNode(Node node, String xpath, NamespaceContext context)
  3. selectString(String xpath, Object node)
  4. selectStrings(String xpath, Object node)
  5. selectXPathString(final String xPath, final Node inNode, final String nsuri, final String pre)
  6. XPathAPI_selectNodeList(Document doc, String xpath, Node namespaceNode)
  7. xpathNode(String expr, Document doc)
  8. xpathNodeList(String expr, Document doc)
  9. xpathOrNull(Document doc, String xp)