|
|
Here is an example of how you can write some trivial code that uses SpartaXml.
(1) Create a test directory and put the sparta.jar file from the download into it. (2) Put the following in a file called foo.xml: <foo> <bar>This one should not be selected.</bar> <bar hello="yes">Hello World!</bar> <bar>This one should not be selected.</bar> </foo> (3) Put the following in a file called SpartaExample.java: import com.hp.hpl.sparta.*; import java.io.*;
/** Example of using SpartaXml */ public class SpartaExample { static public void main(String[] args) { try{ //Get command line arguments if( args.length != 2 ) throw new Error("USAGE: java SpartaExample xmlFile xpath"); File xmlFile = new File(args[0]); String xpath = args[1]; //Parse file to DOM Document Document doc = Parser.parse( xmlFile ); //Find first Element that matches XPath Element match = doc.xpathSelectElement(xpath); //Report result, serializing DOM element to XML if( match == null ) throw new Error("No match for "+xpath+" in "+xmlFile); System.out.println( match.toXml() ); } catch (Exception e){ System.out.println( e.getMessage() ); } } } (4) Compile using: javac -classpath ".;sparta.jar" SpartaExample.java
(5) Run using: javac -classpath ".;sparta.jar" SpartaExample.java You should get the following output: <bar hello="yes">Hello World!</bar> |
|