background image

Running readnwrite Sample

<< Creating the Output Factory | More information about StAX >>
<< Creating the Output Factory | More information about StAX >>
WRITER
S
AMPLE
­ C
URSOR
W
RITER
.
JAVA
115
xtw.writeEndElement();
xtw.writeStartElement("http://www.w3.org/TR/REC-
html40","body");
xtw.writeStartElement("http://www.w3.org/TR/REC-html40","p");
xtw.writeCharacters("Moved to");
xtw.writeStartElement("http://www.w3.org/TR/REC-html40","a");
xtw.writeAttribute("href","http://frob.com");
xtw.writeCharacters("here");
xtw.writeEndElement();
xtw.writeEndElement();
xtw.writeEndElement();
xtw.writeEndElement();
xtw.writeEndDocument();
xtw.flush();
xtw.close();
Running the Sample
When you run the
CursorWriter
sample, the class is compiled, and the XML
stream is parsed as events and written to a file named
CursorWriter-Output
:
<!--all elements here are explicitly in the HTML namespace-->
<?xml version="1.0" encoding="utf-8"?>
<html:html xmlns:html="http://www.w3.org/TR/REC-html40">
<html:head>
<html:title>Frobnostication</html:title></html:head>
<html:body>
<html:p>Moved to <html:a href="http://frob.com">here</html:a>
</html:p>
</html:body>
</html:html>
Note that in the actual
CursorWriter-Output
file, this stream is written without
any linebreaks; the breaks have been added here to make the listing easier to
read. In this example, as with the object stream in the
event Sample ­ Event-
Parse.java
sample, the namespace prefix is added to both the opening and clos-
ing HTML tags. This is not required by the StAX specification, but it is good
practice when the final scope of the output stream is not definitively known.