[ Pobierz całość w formacie PDF ]
x = te.getException();
x.printStackTrace();
} catch (SAXException sxe) {
...
Notes:
http://java.sun.com/xml/jaxp-1.1/docs/tutorial/xslt/2_write.html (3 of 6) [8/22/2001 12:52:18 PM]
2. Writing a DOM
TransformerExceptions are thrown by the transformer object.
TransformerConfigurationExceptions are thrown by the factory.
Addendum:
Astute reader Malcolm Gorman points out that, as it is currently written, the transformation app won't preserve athe
XML document's DOCTYPE setting. He proposes the following code to remedy the omission:
String systemValue = (new File(document.getDoctype().getSystemId())).getName();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemValue);
Writing the XML
For instructions on how to compile and run the program, see Compiling the Program and Running the Program, from the SAX
tutorial. (Substitute "TransformationApp" for "Echo" as the name of the program.)
When you run the program on slideSample01.xml, this is the output you see:
Wake up to WonderWidgets!
Overview
Why
WonderWidgets are great
Who
buys WonderWidgets
Note:
See Reading XML into a DOM, Additional Information to find out more about configuring the factory and handling
validation errors.
http://java.sun.com/xml/jaxp-1.1/docs/tutorial/xslt/2_write.html (4 of 6) [8/22/2001 12:52:18 PM]
2. Writing a DOM
Writing Out a Subtree of the DOM
It is also possible to operate on a subtree of a DOM. In this section of the tutorial, you'll experiment with that option.
Note:
The code discussed in this section is in TransformationApp03.java. The output is in TransformationLog03.
The only difference in the process is that now you will create a DOMSource using a node in the DOM, rather than the entire DOM.
The first step will be to import the classes you need to get the node you want. Add the code highlighted below to do that:
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
The next step is to find a good node for the experiment. Add the code highlighted below to select the first element:
try {
File f = new File(argv[0]);
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(f);
// Get the first element in the DOM
NodeList list = document.getElementsByTagName("slide");
Node node = list.item(0);
Finally, make the changes shown below to construct a source object that consists of the subtree rooted at that node:
DOMSource source = new DOMSource(document);
DOMSource source = new DOMSource(node);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
Now run the app. Your output should look like this:
Wake up to WonderWidgets!
Clean Up
Because it will be easiest to do now, make the changes shown below to back out the additions you made in this section.
(TransformationApp04.java contains these changes.)
Import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
...
try {
...
// Get the first element in the DOM
NodeList list = document.getElementsByTagName("slide");
Node node = list.item(0);
http://java.sun.com/xml/jaxp-1.1/docs/tutorial/xslt/2_write.html (5 of 6) [8/22/2001 12:52:18 PM]
2. Writing a DOM
...
DOMSource source = new DOMSource(node);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
Summary
At this point, you've seen how to use a transformer to write out a DOM, and how to use a subtree of a DOM as the source object in a
transformation. In the next section, you'll see how to use a transformer to create XML from any data structure you are capable of
parsing.
Top Contents Index Glossary
http://java.sun.com/xml/jaxp-1.1/docs/tutorial/xslt/2_write.html (6 of 6) [8/22/2001 12:52:18 PM]
1. Intro to XSLT
Top Contents Index Glossary
1. Introducing XSLT and XPath
The XML Stylesheet Language (XSL) has three major
Link Summary
subcomponents:
Local Links
XSL-FO
Generating XML from an
The "flow object" standard. By far the largest
Arbitrary Data Structure
subcomponent, this standard gives mechanisms for
Transforming XML Data with
describing font sizes, page layouts, and how
XSLT
information "flows" from one page to another. This
subcomponent is not covered by JAXP, nor is it
included in this tutorial.
External Links
XSLT
XPath Specification
This the transformation language, which lets you
transform XML into some other format. For example,
Glossary Terms
you might use XSLT to produce HTML, or a different
XML structure. You could even use it to produce plain
DOM, SAX, URI, XSL
text or to put the information in some other document
format. (And as you'll see in Generating XML from an
Arbitrary Data Structure, a clever application can press
it into service to manipulate non-XML data, as well.)
XPath
At bottom, XSLT is a language that lets you specify what sorts of things to do when a particular
element is encountered. But to write a program for different parts of an XML data structure, you
need to be able to specify the part of the structure you are talking about at any given time. XPath
is that specification language. It is an addressing mechanism that lets you specify a path to an
element so, for example, can be distinguished from . That way,
you can describe different kinds of translations for the different elements.
The remainder of this section describes the XSLT package structure, and discusses the XPath addressing
mechanism in a bit more depth.
http://java.sun.com/xml/jaxp-1.1/docs/tutorial/xslt/1_intro.html (1 of 10) [8/22/2001 12:52:20 PM]
1. Intro to XSLT
The XSLT Packages
There XSLT packages break down as follows:
javax.xml.transform
[ Pobierz całość w formacie PDF ]