XML Programming Notes


Index


General information

Here is a collection of pointers to relevant standards and tutorials on the Web:


Writing and validating XML

You can use your favorite editor to edit XML documents. The file name should have suffix .xml. Emacs has a descent SGML mode for editing XML, which is automatically invoked for file names with .xml suffix. There are also many specialized XML editors available, and some of them come with nice templates and automatic validation. Just search for "XML editor" in Google.

Some XML and DTD examples can be found on rack40 in ~cps216/examples/xml-zthes/.

To check that your XML document is well-formed (i.e., no synatx errors), use the command java -classpath ~cps216/examples/jaxp SaxEcho file on rack40, where file is the file name of the XML document. The command will echo the parsed XML to stdout.

To validate your XML document against a DTD (you will need a <!DOCTYPE declaration in the XML document), use the command java -classpath ~cps216/examples/jaxp SaxEcho -v file. The additional -v flag turns on validation.

If you edit XML on a Windows machine, you can fire up Internet Explorer and use it to open your XML document. IE will automatically validate the XML document and display it in a nice format.


Running XPath and XQuery in QuiP

QuiP is an XQuery engine by Software AG. You can run QuiP either in batch mode or with its GUI. The GUI is recommended for testing your queries since it provides synatx highlighting for XQuery and displays query results in a nice tree format. To run the GUI on rack40, type ~cps216/QuiP/QuipGui.sh &. The interface is pretty self-explanatory. To start, type in the following query and click on "EXECUTE":

  <result>
    { document("/home/cps216/QuiP/examples/theater/othello.xml")//PERSONA }
  </result>

To run QuiP in batch mode, type ~cps216/QuiP/quip xquery_file, where xquery_file is the name of a text file containing your XQuery. The result will be output to stdout.


Using XSLT

On rack40, use the command java -classpath ~cps216/examples/jaxp Stylizer xsltFile xmlFile to transform input xmlFile using the XSLT stylesheet xsltFile. The result document will be output to stdout.

You can read the code in ~cps216/examples/jaxp/Stylizer.java for an example of automatically applying XSLT transformations inside Java code.


Coding with JAXP

Plenty of examples can be found in ~cps216/examples/jaxp/ on rack40. SaxEcho.java uses an SAX parser to parse and validate an input XML document, and uses SAX API to echo the input to stdout. DomEcho.java uses a DOM parser to parse and validate an input XML document and to build an in-memory DOM representation of the input; it then uses DOM API to print the DOM tree out as XML. DomTree.java prints out the DOM tree representation directly, which can be useful in debugging. The standard way of converting DOM into XML for output is to use a default Transformer, which is illustrated in Stylizer.java. Stylizer.java also shows how to use Transformer to perform XSLT transformations.

On rack40, your CLASSPATH is already set up to include the standard JDK library (and hence the JAXP library). Therefore, you can simply use javac *.java to compile your code. However, since the standard CLASSPATH does not include the current directory, you may need to use java -classpath . JavaClassName in the directory containing JavaClassName.class.