KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > Test


1 package demo;
2
3 import java.util.Iterator JavaDoc;
4 import org.w3c.dom.Node JavaDoc;
5 import org.apache.commons.jxpath.Pointer;
6 import org.enhydra.xml.xmlc.XMLObject;
7
8 /**
9  * Demo how to set node value using XPath
10  */

11 public class Test extends XPath
12 {
13     private String JavaDoc path;
14     private String JavaDoc value;
15     private String JavaDoc file;
16
17     public Test (String JavaDoc f, String JavaDoc p, String JavaDoc v) {
18     file = f;
19     path = p;
20     value = v;
21     }
22
23     public void run() {
24         XMLObject xmlObject = null;
25
26         xmlObject = getFactory().createFromFile(file);
27         System.out.println("\nJXPath Result...");
28         runJXPath(xmlObject);
29         System.out.println (xmlObject.toDocument());
30
31         xmlObject = getFactory().createFromFile(file);
32         System.out.println("\nJaxen Result...");
33         runJaxen(xmlObject);
34         System.out.println (xmlObject.toDocument());
35     }
36
37     public void runJXPath(XMLObject xmlObject) {
38     for (Iterator JavaDoc iter = query(xmlObject, path, XPath.XPATH_IMPL_JXPATH); iter.hasNext() ; ) {
39         Pointer pointer = (Pointer)iter.next();
40
41         System.out.println ("Change " + pointer + " from '" + pointer.getValue() + "' to '" + value + "'");
42         pointer.setValue(value);
43     }
44     }
45
46     public void runJaxen(XMLObject xmlObject) {
47     for (Iterator JavaDoc iter = query(xmlObject, path, XPath.XPATH_IMPL_JAXEN); iter.hasNext() ; ) {
48         Node JavaDoc node = (Node JavaDoc)iter.next();
49         String JavaDoc nodeValue = node.getNodeValue();
50             if (nodeValue == null) {
51             node = node.getFirstChild();
52             nodeValue = node.getNodeValue();
53             }
54
55         System.out.println ("Change " + node + " from '" + nodeValue + "' to '" + value + "'");
56         node.setNodeValue(value);
57     }
58     }
59
60     public static void main (String JavaDoc args[]) {
61     if (args.length == 3) {
62         Test test = new Test(args[0], args[1], args[2]);
63         test.run();
64     } else {
65         System.err.println ("Test file path newValue");
66     }
67     }
68
69
70 }
71
Popular Tags