1 7 8 package org.dom4j.xpath; 9 10 import junit.textui.TestRunner; 11 12 import java.util.Iterator ; 13 import java.util.List ; 14 15 import org.dom4j.AbstractTestCase; 16 import org.dom4j.DocumentHelper; 17 import org.dom4j.Node; 18 import org.dom4j.NodeFilter; 19 20 26 public class FilterTest extends AbstractTestCase { 27 protected static String [] paths = {".[name()='author']", 28 ".[name()='XXXX']", ".[.='James Strachan']", ".[.='XXXX']"}; 29 30 public static void main(String [] args) { 31 TestRunner.run(FilterTest.class); 32 } 33 34 public void testXPaths() throws Exception { 37 int size = paths.length; 38 39 for (int i = 0; i < size; i++) { 40 testXPath(paths[i]); 41 } 42 } 43 44 protected void testXPath(String xpathExpression) { 47 NodeFilter nodeFilter = DocumentHelper 48 .createXPathFilter(xpathExpression); 49 assertTrue("No NodeFilter object was created", nodeFilter != null); 50 51 List list = document.selectNodes("//author"); 52 53 for (Iterator iter = list.iterator(); iter.hasNext();) { 54 Node node = (Node) iter.next(); 55 56 if (nodeFilter.matches(node)) { 57 log("Matches node: " + node.asXML()); 58 } else { 59 log("No match for node: " + node.asXML()); 60 } 61 } 62 } 63 } 64 65 101 | Popular Tags |