XML Output

Examples

To create XML using Xml Output you will need to instantiate an implementation of the XmlOutput interface and provide it with an implementation of the XmlSink interface. There is currently only one implementation of XmlOutput: XmlValidator, and there are two XmlSink implementations: TextSink and SaxSink.

To write XML to a character stream:

The following code...

...
final String encoding = "utf-8";
Writer writer = new OutputStreamWriter(System.out, encoding);
XmlOutput out = new XmlValidator(new TextSink(writer, encoding, false, true));

out.startElement("xmloutput");
out.startElement("is");
out.attribute("very", "cool");
out.endElement();
out.endElement();
out.close();

writer.flush();
writer.close();
...

Produces the following output...

<?xml version="1.0" encoding="utf-8"?>
<xmloutput>
    <is very="cool"/>
</xmloutput>

Using namespaces:

The following code...

...
final String encoding = "utf-8";
Writer writer = new OutputStreamWriter(System.out, encoding);
XmlOutput out = new XmlValidator(new TextSink(writer, encoding, false, true));

out.startElement("exmpl", "xmloutput");
out.namespace("exmpl", "http://www.labradortechnologies.com/xmlns/example");
out.startElement("exmpl", "is");
out.attribute("exmpl", "very", "cool");
out.endElement();
out.endElement();

writer.flush();
writer.close();
...

Produces the following output...

<?xml version="1.0" encoding="utf-8"?>
<exmpl:xmloutput xmlns:exmpl="http://www.labradortechnologies.com/xmlns/example">
    <exmpl:is very="cool"/>
</exmpl:xmloutput>