Chapter 11. Java and the XML DOM

This chapter is all about using XML with Java to create standalone programs. I'll even create a few browsers in this chapter. As of Java 1.4, the current version at this time, Java includes a great deal of support for working with XML; I'll use Java 1.4 in this and the next chapter.

If you're using a version of Java before 1.4, you should install Java 1.4 to run the examples in this chapter and the next one. Alternatively, you can download and install the Java XML pack; currently, the download page is http://java.sun.com/xml/downloads/javaxmlpack.html (if that page doesn't exist when you read this, go to http://java.sun.com/xml). After downloading and unzipping, you must include the files jaxp-api.jar and xalan.jar in the CLASSPATH environment variable to run the Java XML examples coming up in this book (a JAR file holds a Java Archive, full of compressed .class files). The CLASSPATH variable tells Java where to search for .class files; when it is set it to the names of these JAR files, Java can find the .class files it needs.

Setting the CLASSPATH variable is just like setting the PATH environment variable you saw in the previous chapter. For example, if jaxp-api.jar and xalan.jar are in the current directory, you can set the CLASSPATH variable this way in Windows:

 C:\>SET CLASSPATH=jaxp-api.jar;xalan.jar 

If jaxp-api.jar and xalan.jar are in another directory, such as c:\javaxmlpack, you must include that path when setting the CLASSPATH variable:

 C:\>SET CLASSPATH=c:\javaxmlpack\jaxp-api.jar;c:\javaxmlpack\xalan.jar 

You can also set the CLASSPATH variable permanently, just as you could with the PATH variable in the previous chaptersee http://java.sun.com/j2se/1.4.1/install.html for instructions on how to set environment variables such as CLASSPATH permanently.

There's a shortcut if you can't get the CLASSPATH variable working: You can use the -classpath "switch" when working with the javac and java tools. For example, here's how I compile and run a program named browser.java using that switch to specify the classpath I want to use:

 %javac -classpath c:\javaxmlpack\jaxp-api.jar;c:\javaxmlpack\xalan.jar browser.java 

You don't need to do any of this if you have Java 1.4 installedyou won't need the Java XML Pack until Chapter 18, "SOAP and RDF," where we discuss the XML SOAP. We're ready to start working with code now. I'll start by writing an example that parses an XML document.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net