Reputation: 181
I can write this XML using Java DOM:
<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
<cac:Employee id="1">
<cbc:name>Pankaj</cbc:name>
<cbc:age>29</cbc:age>
<cbc:role>Java Developer</cbc:role>
<cbc:gender>Male</cbc:gender>
</cac:Employee>
<cac:Employee id="2">
<cbc:name>Lisa</cbc:name>
<cbc:age>35</cbc:age>
<cbc:role>Manager</cbc:role>
<cbc:gender>Female</cbc:gender>
</cac:Employee>
</Invoice>
But, I'm wondering if I can write this XML using the Saxon Library. Saxon library is a powerful library to do things like XPath evaluation, fast reading, XSLT transformation, and so on, but I didn't found any example of how to write and XML with that library. Is it possible? If were possible, could you give me any suggestions?
Upvotes: 0
Views: 296
Reputation: 163262
There's actually a range of mechanisms, some quite well established and others relatively new.
You can construct a Serializer
using Processor.newSerializer()
, and then get either a SAX ContentHandler
(with Serializer.getContentHandler
) or a StAX XmlStreamWriter
(with Serializer.getXMLStreamWriter
) enabling you to write to the serializer an event at a time. These mechanisms have the advantage that the API is standard, but I don't find either of them very user-friendly.
More recently I introduced the Push API, which has more of a "fluent API" feel to it, in that when you start an element node, it returns a handle that you can use for writing the children and attributes of the element.
Also new, and a little experimental, is the Sapling tree. This constructs a lightweight immutable tree in memory, which can then be written out to a Serializer when you're finished.
For both these newer APIs, the main documentation is the Javadoc.
Upvotes: 1