[ Pobierz całość w formacie PDF ]
Creating and Manipulating a DOM
Compiling the Program
Note:
Running the Program
In the next part of the tutorial, Using XSLT, you'll see how to
Handling Errors
write out a DOM as an XML file. (You'll also see how to
convert an existing data file into XML with relative ease.) Using the Validating Parser
Using Namespaces
Using XSLT
Creating the Program
Exercise Links
The Document Object Model (DOM) provides APIs that let you
create nodes, modify them, delete and rearrange them. So it is
relatively easy to create a DOM, as you'll see in later in section 5 of
DomEcho01.java
this tutorial, Creating and Manipulating a DOM.
slideSample01.xml
slideSample01-xml.html
Before you try to create a DOM, however, it is helpful to understand
how a DOM is structured. This series of exercises will make DOM
API Links
internals visible by displaying them in a Swing JTree.
DocumentBuilder
Create the Skeleton
Document
Now that you've had a quick overview of how to create a DOM, let's
External Links
build a simple program to read an XML document into a DOM then
write it back out again.
Level 1 DOM specification
Note:
Glossary Terms
The code discussed in this section is in DomEcho01.java. The
file it operates on is slideSample01.xml. (The browsable
DOM, namespace, SAX, URI,
version is slideSample01-xml.html.)
validating parser
Start with a normal basic logic for an app, and check to make sure
that an argument has been supplied on the command line:
http://java.sun.com/xml/jaxp-1.1/docs/tutorial/dom/1_read.html (1 of 6) [8/22/2001 12:52:25 PM]
1. Reading Data into a DOM
public class DomEcho {
public static void main(String argv[])
{
if (argv.length != 1) {
System.err.println("Usage: java DomEcho filename");
System.exit(1);
}
}// main
}// DomEcho
Import the Required Classes
In this section, you're going to see all the classes individually named. That's so you can see where each class comes
from when you want to reference the API documentation. In your own apps, you may well want to replace import
statements like those below with the shorter form: javax.xml.parsers.*.
Add these lines to import the JAXP APIs you'll be using:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
Add these lines for the exceptions that can be thrown when the XML document is parsed:
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
Add these lines to read the sample XML file and identify errors:
import java.io.File;
import java.io.IOException;
Finally, import the W3C definition for a DOM and DOM exceptions:
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
Note:
DOMExceptions are only thrown when traversing or manipulating a DOM. Errors that occur during
parsing are reporting using a different mechanism that is covered below.
Declare the DOM
The org.w3c.dom.Document class is the W3C name for a Document Object Model (DOM). Whether you parse an
http://java.sun.com/xml/jaxp-1.1/docs/tutorial/dom/1_read.html (2 of 6) [8/22/2001 12:52:25 PM]
1. Reading Data into a DOM
XML document or create one, a Document instance will result. We'll want to reference that object from another
method later on in the tutorial, so define it as a global object here:
public class DomEcho
{
static Document document;
public static void main(String argv[])
{
It needs to be static, because you're going to you generate it's contents from the main method in a few minutes.
Handle Errors
Next, put in the error handling logic. This code is very similar to the logic you saw in Handling Errors in the SAX
tutorial, so we won't go into it in detail here. The major point worth noting is that a JAXP-conformant document
builder is required to report SAX exceptions when it has trouble parsing the XML document. The DOM parser does
not have to actually use a SAX parser internally, but since the SAX standard was already there, it seemed to make
sense to use it for reporting errors. As a result, the error-handling code for DOM and SAX applications are very
similar:
public static void main(String argv[])
{
if (argv.length != 1) {
...
}
try {
} catch (SAXException sxe) {
// Error generated during parsing)
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();
} catch (IOException ioe) {
// I/O error
ioe.printStackTrace();
}
}// main
The major difference between this code and the SAX error-handling code is that the DOM parser does not throw
SAXParseException's, but only SAXException's.
http://java.sun.com/xml/jaxp-1.1/docs/tutorial/dom/1_read.html (3 of 6) [8/22/2001 12:52:25 PM]
1. Reading Data into a DOM
Instantiate the Factory
Next, add the code highlighted below to obtain an instance of a factory that can give us a document builder:
public static void main(String argv[])
{
if (argv.length != 1) {
...
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
Get a Parser and Parse the File
Now, add the code highlighted below to get a instance of a builder, and use it to parse the specified file:
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse( new File(argv[0]) );
} catch (SAXParseException spe) {
Save This File!
By now, you should be getting the idea that every JAXP application starts pretty much the same
way. You're right! Save this version of the file as a template. You'll use it later on as the basis for
XSLT transformation app.
[ Pobierz całość w formacie PDF ]