Sampe usage:
XmlWriter w = new XmlWriter(); w.start(DOC_TAG, "xmlns", DOC_NAMESPACE); if (foo != null) w.start(FOO_TAG).value(foo).end(); w.end();
Note that implementations can have different operating modes: specifically, when dealing with illegal content (such as "--" in a comment, "?>" in processing instruction, or "]]>" within CDATA section), implementations can do one of 3 things:
The XmlWriter class exposes a number of protected methods that enable it to be subclassed for the purposes of customizing its output. See {@link com.google.javascript.util.JsonWriter} for an example.
new XmlWriter(sw, EnumSet.of(WriterFlags.WRITE_HEADER, WriterFlags.EXPAND_EMPTY, WriterFlags.PRETTY_PRINT), null)The caller can supply any of the values enumerated in {@link XmlWriter.WriterFlags}, or none. Once a feature has been enabled in the constructor, it cannot be turned off.
<?xml version='1.0'?>
<foo> <wee really="yeah"></wee> </foo>
The {@code PRETTY_PRINT} flag enables pretty printing. This featureformats the XML output with using new lines and tab characters:
<foo> <bar> <wee really="yeah"/> </bar> </foo>
Will produce wonky formatting:
w.startElement(null, "txt", null, null); w.simpleElement(null, "fooey", null, null); w.characters("Kleenex"); w.endElement(null, "txt");
<txt> <fooey/>Kleenex </txt>
You can ensure correct formatting of mixed content in your document by using the {@link #innerXml(String)} method to write raw XML.
Correctly formatted:
w.startElement(null, "txt", null, null); w.innerXml("<fooey/>"); w.characters("Kleenex"); w.endElement(null, "txt");
<txt><fooey/>Kleenex</txt>
Application writers should keep in mind that this class does not perform any sort of coherency check, and will not prevent them from closing elements they haven't opened yet, or any other thing that would make the XML output invalid.
@author Nicolas RinaudoThe resulting document will look like this:
<?xml version="1.0" standalone="yes"?> <_NS1:foo xmlns:_NS1="http://www.foo.com/ns/"/>
In many cases, document authors will prefer to choose their own prefixes rather than using the (ugly) default names. The XML writer allows two methods for selecting prefixes:
Whenever the XML writer finds a new Namespace URI, it checks to see if a qualified (prefixed) name is also available; if so it attempts to use the name's prefix (as long as the prefix is not already in use for another Namespace URI).
The resulting document will look like this:
<?xml version="1.0" standalone="yes"?> <foo:foo xmlns:foo="http://www.foo.com/ns/"/>
The default Namespace simply uses an empty string as the prefix:
w.setPrefix("http://www.foo.com/ns/", ""); w.startDocument(); w.emptyElement("http://www.foo.com/ns/", "foo"); w.endDocument();
The resulting document will look like this:
<?xml version="1.0" standalone="yes"?> <foo xmlns="http://www.foo.com/ns/"/>
By default, the XML writer will not declare a Namespace until it is actually used. Sometimes, this approach will create a large number of Namespace declarations, as in the following example:
<xml version="1.0" standalone="yes"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description about="http://www.foo.com/ids/books/12345"> <dc:title xmlns:dc="http://www.purl.org/dc/">A Dark Night</dc:title> <dc:creator xmlns:dc="http://www.purl.org/dc/">Jane Smith</dc:title> <dc:date xmlns:dc="http://www.purl.org/dc/">2000-09-09</dc:title> </rdf:Description> </rdf:RDF>
The "rdf" prefix is declared only once, because the RDF Namespace is used by the root element and can be inherited by all of its descendants; the "dc" prefix, on the other hand, is declared three times, because no higher element uses the Namespace. To solve this problem, you can instruct the XML writer to predeclare Namespaces on the root element even if they are not used there:
w.forceNSDecl("http://www.purl.org/dc/");
Now, the "dc" prefix will be declared on the root element even though it's not needed there, and can be inherited by its descendants:
<xml version="1.0" standalone="yes"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://www.purl.org/dc/"> <rdf:Description about="http://www.foo.com/ids/books/12345"> <dc:title>A Dark Night</dc:title> <dc:creator>Jane Smith</dc:title> <dc:date>2000-09-09</dc:title> </rdf:Description> </rdf:RDF>
This approach is also useful for declaring Namespace prefixes that be used by qualified names appearing in attribute values or character data.
@author David Megginson, david@megginson.com @version 0.2 @since JAXB1.0 @see org.xml.sax.XMLFilter @see org.xml.sax.ContentHandlerThe resulting document will look like this:
<?xml version="1.0" standalone="yes"?> <_NS1:foo xmlns:_NS1="http://www.foo.com/ns/"/>
In many cases, document authors will prefer to choose their own prefixes rather than using the (ugly) default names. The XML writer allows two methods for selecting prefixes:
Whenever the XML writer finds a new Namespace URI, it checks to see if a qualified (prefixed) name is also available; if so it attempts to use the name's prefix (as long as the prefix is not already in use for another Namespace URI).
The resulting document will look like this:
<?xml version="1.0" standalone="yes"?> <foo:foo xmlns:foo="http://www.foo.com/ns/"/>
The default Namespace simply uses an empty string as the prefix:
w.setPrefix("http://www.foo.com/ns/", ""); w.startDocument(); w.emptyElement("http://www.foo.com/ns/", "foo"); w.endDocument();
The resulting document will look like this:
<?xml version="1.0" standalone="yes"?> <foo xmlns="http://www.foo.com/ns/"/>
By default, the XML writer will not declare a Namespace until it is actually used. Sometimes, this approach will create a large number of Namespace declarations, as in the following example:
<xml version="1.0" standalone="yes"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description about="http://www.foo.com/ids/books/12345"> <dc:title xmlns:dc="http://www.purl.org/dc/">A Dark Night</dc:title> <dc:creator xmlns:dc="http://www.purl.org/dc/">Jane Smith</dc:title> <dc:date xmlns:dc="http://www.purl.org/dc/">2000-09-09</dc:title> </rdf:Description> </rdf:RDF>
The "rdf" prefix is declared only once, because the RDF Namespace is used by the root element and can be inherited by all of its descendants; the "dc" prefix, on the other hand, is declared three times, because no higher element uses the Namespace. To solve this problem, you can instruct the XML writer to predeclare Namespaces on the root element even if they are not used there:
w.forceNSDecl("http://www.purl.org/dc/");
Now, the "dc" prefix will be declared on the root element even though it's not needed there, and can be inherited by its descendants:
<xml version="1.0" standalone="yes"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://www.purl.org/dc/"> <rdf:Description about="http://www.foo.com/ids/books/12345"> <dc:title>A Dark Night</dc:title> <dc:creator>Jane Smith</dc:title> <dc:date>2000-09-09</dc:title> </rdf:Description> </rdf:RDF>
This approach is also useful for declaring Namespace prefixes that be used by qualified names appearing in attribute values or character data.
@author David Megginson, david@megginson.com @version 0.2 @since JAXB1.0 @see org.xml.sax.XMLFilter @see org.xml.sax.ContentHandlerThis interface provides simple methods to write XML data onto a writer.
Most implementation should wrap a writer or an output stream. Implementations can be focused on performance, reliability, error reporting, etc...
For improved performance, the most efficient solution will generally to have an implementation write on a buffered writer since the memory usage will generally be restricted little more than the size of the buffer, and this will keep the I/O operation to a minimum.
Other implementations might want to wrap a SAX content handler. @author Christophe Lauret (Allette Systems) @version 11 December 2011
Calling indent
and outdent
changes the indentation level. The default indentation width is 4 spaces.
@author Jim Menard, jimm@io.com
Lightweight {@link IXmlWriter} implementation.
Requires a wrapper to be used safely in a multithreaded environment.
Not intended to be subclassed. Please copy and hack!
A simple serializer for XML documents.
@author Jochen WiedmannSerialize an xml representation of a graph of objects into a writer.
Created: Feb 24, 2003
Copyright: Copyright (c) 2003
Assumptions: none
Requires: nothing
Required by: nothing
Revision History:
Example:
Config config = new Config(); config.setNumber(123); config.setString("acb"); XmlWriter writer = new XmlWriter("filename.xml"); writer.setRootObject(config); Would produce an xml file that might look like: <Config> <number>123</number> <string>abc</string> </Config>
Conventions:
The resulting document will look like this:
<?xml version="1.0" standalone="yes"?> <_NS1:foo xmlns:_NS1="http://www.foo.com/ns/"/>
In many cases, document authors will prefer to choose their own prefixes rather than using the (ugly) default names. The XML writer allows two methods for selecting prefixes:
Whenever the XML writer finds a new Namespace URI, it checks to see if a qualified (prefixed) name is also available; if so it attempts to use the name's prefix (as long as the prefix is not already in use for another Namespace URI).
Before writing a document, the client can also pre-map a prefix to a Namespace URI with the setPrefix method:
w.setPrefix("http://www.foo.com/ns/", "foo"); w.startDocument(); w.emptyElement("http://www.foo.com/ns/", "foo"); w.endDocument();
The resulting document will look like this:
<?xml version="1.0" standalone="yes"?> <foo:foo xmlns:foo="http://www.foo.com/ns/"/>
The default Namespace simply uses an empty string as the prefix:
w.setPrefix("http://www.foo.com/ns/", ""); w.startDocument(); w.emptyElement("http://www.foo.com/ns/", "foo"); w.endDocument();
The resulting document will look like this:
<?xml version="1.0" standalone="yes"?> <foo xmlns="http://www.foo.com/ns/"/>
By default, the XML writer will not declare a Namespace until it is actually used. Sometimes, this approach will create a large number of Namespace declarations, as in the following example:
<xml version="1.0" standalone="yes"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description about="http://www.foo.com/ids/books/12345"> <dc:title xmlns:dc="http://www.purl.org/dc/">A Dark Night</dc:title> <dc:creator xmlns:dc="http://www.purl.org/dc/">Jane Smith</dc:title> <dc:date xmlns:dc="http://www.purl.org/dc/">2000-09-09</dc:title> </rdf:Description> </rdf:RDF>
The "rdf" prefix is declared only once, because the RDF Namespace is used by the root element and can be inherited by all of its descendants; the "dc" prefix, on the other hand, is declared three times, because no higher element uses the Namespace. To solve this problem, you can instruct the XML writer to predeclare Namespaces on the root element even if they are not used there:
w.forceNSDecl("http://www.purl.org/dc/");
Now, the "dc" prefix will be declared on the root element even though it's not needed there, and can be inherited by its descendants:
<xml version="1.0" standalone="yes"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://www.purl.org/dc/"> <rdf:Description about="http://www.foo.com/ids/books/12345"> <dc:title>A Dark Night</dc:title> <dc:creator>Jane Smith</dc:title> <dc:date>2000-09-09</dc:title> </rdf:Description> </rdf:RDF>
This approach is also useful for declaring Namespace prefixes that be used by qualified names appearing in attribute values or character data.
@author David Megginson, david@megginson.com @version 0.2 @see org.xml.sax.XMLFilter @see org.xml.sax.ContentHandler XMLWriter
takes a DOM4J tree and formats it to a stream as XML. It can also take SAX events too so can be used by SAX clients as this object implements the {@link org.xml.sax.ContentHandler}and {@link LexicalHandler} interfaces. as well. This formatter performs typical documentformatting. The XML declaration and processing instructions are always on their own lines. An {@link OutputFormat}object can be used to define how whitespace is handled when printing and allows various configuration options, such as to allow suppression of the XML declaration, the encoding declaration or whether empty documents are collapsed.
There are write(...)
methods to print any of the standard DOM4J classes, including Document
and Element
, to either a Writer
or an OutputStream
. Warning: using your own Writer
may cause the writer's preferred character encoding to be ignored. If you use encodings other than UTF8, we recommend using the method that takes an OutputStream instead.
The resulting document will look like this:
<?xml version="1.0" standalone='yes'?> <_NS1:foo xmlns:_NS1="http://www.foo.com/ns/"/>
In many cases, document authors will prefer to choose their own prefixes rather than using the (ugly) default names. The XML writer allows two methods for selecting prefixes:
Whenever the XML writer finds a new Namespace URI, it checks to see if a qualified (prefixed) name is also available; if so it attempts to use the name's prefix (as long as the prefix is not already in use for another Namespace URI).
Before writing a document, the client can also pre-map a prefix to a Namespace URI with the setPrefix method:
w.setPrefix("http://www.foo.com/ns/", "foo"); w.startDocument(); w.emptyElement("http://www.foo.com/ns/", "foo"); w.endDocument();
The resulting document will look like this:
<?xml version="1.0" standalone='yes'?> <foo:foo xmlns:foo="http://www.foo.com/ns/"/>
The default Namespace simply uses an empty string as the prefix:
w.setPrefix("http://www.foo.com/ns/", ""); w.startDocument(); w.emptyElement("http://www.foo.com/ns/", "foo"); w.endDocument();
The resulting document will look like this:
<?xml version="1.0" standalone='yes'?> <foo xmlns="http://www.foo.com/ns/"/>
By default, the XML writer will not declare a Namespace until it is actually used. Sometimes, this approach will create a large number of Namespace declarations, as in the following example:
<xml version="1.0" standalone='yes'?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description about="http://www.foo.com/ids/books/12345"> <dc:title xmlns:dc="http://www.purl.org/dc/">A Dark Night</dc:title> <dc:creator xmlns:dc="http://www.purl.org/dc/">Jane Smith</dc:title> <dc:date xmlns:dc="http://www.purl.org/dc/">2000-09-09</dc:title> </rdf:Description> </rdf:RDF>
The "rdf" prefix is declared only once, because the RDF Namespace is used by the root element and can be inherited by all of its descendants; the "dc" prefix, on the other hand, is declared three times, because no higher element uses the Namespace. To solve this problem, you can instruct the XML writer to predeclare Namespaces on the root element even if they are not used there:
w.forceNSDecl("http://www.purl.org/dc/");
Now, the "dc" prefix will be declared on the root element even though it's not needed there, and can be inherited by its descendants:
<xml version="1.0" standalone='yes'?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://www.purl.org/dc/"> <rdf:Description about="http://www.foo.com/ids/books/12345"> <dc:title>A Dark Night</dc:title> <dc:creator>Jane Smith</dc:title> <dc:date>2000-09-09</dc:title> </rdf:Description> </rdf:RDF>
This approach is also useful for declaring Namespace prefixes that be used by qualified names appearing in attribute values or character data.
This mode, enabled by the "dataFormat" property, pretty-prints field-oriented XML without mixed content. All added indentation and newlines will be passed on down the filter chain (if any).
In general, all whitespace in an XML document is potentially significant, so a general-purpose XML writing tool cannot add newlines or indentation.
There is, however, a large class of XML documents where information is strictly fielded: each element contains either character data or other elements, but not both. For this special case, it is possible for a writing tool to provide automatic indentation and newlines without requiring extra work from the user. Note that this class will likely not yield appropriate results for document-oriented XML like XHTML pages, which mix character data and elements together.
This writer mode will automatically place each start tag on a new line, optionally indented if an indent step is provided (by default, there is no indentation). If an element contains other elements, the end tag will also appear on a new line with leading indentation. Consider, for example, the following code:
XmlWriter w = new XmlWriter(); w.setDataFormat(true); w.setIndentStep(2); w.startDocument(); w.startElement("Person"); w.dataElement("name", "Jane Smith"); w.dataElement("date-of-birth", "1965-05-23"); w.dataElement("citizenship", "US"); w.endElement("Person"); w.endDocument();
This code will produce the following document:
<?xml version="1.0" standalone='yes'?> <Person> <name>Jane Smith</name> <date-of-birth>1965-05-23</date-of-birth> <citizenship>US</citizenship> </Person>@see org.xml.sax.XMLFilter @see org.xml.sax.ContentHandler @author David Megginson, Jerome Louvel (contact@noelios.com)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|