1 package demo; 2 3 import java.util.Iterator ; 4 import org.w3c.dom.Node ; 5 import org.apache.commons.jxpath.Pointer; 6 import org.enhydra.xml.xmlc.XMLObject; 7 8 11 public class Test extends XPath 12 { 13 private String path; 14 private String value; 15 private String file; 16 17 public Test (String f, String p, String 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 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 iter = query(xmlObject, path, XPath.XPATH_IMPL_JAXEN); iter.hasNext() ; ) { 48 Node node = (Node )iter.next(); 49 String 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 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 |