Simple way to generate XML documents and schemas using Java, but feeling like Clojure.
Requires Java 7 or greater.
Get this library from Maven Central using the following dependency:
<dependency>
<groupId>fi.solita.datatree</groupId>
<artifactId>datatree</artifactId>
<version>1.0.0</version>
</dependency>
Add static imports for the factory methods in the Tree
class:
import static fi.solita.datatree.Tree.*;
Now you can describe an XML document as a series of nested calls to tree
and
meta
. Here are some examples:
tree("element", "some text")
becomes <element>some text</element>
tree("element", meta("attribute", "value"))
becomes <element attribute="value"/>
tree("outer", tree("inner-1"), tree("inner-2"))
becomes <outer><inner-1/><inner-2/></outer>
To convert a tree into XML, you can use one of the various methods in XmlGenerator
:
Tree tree = tree("some-tree");
InputStream in = XmlGenerator.toInputStream(tree);
There are helper methods in XmlSchema
for generating XML
Schemas. First do a static import for them:
import static fi.solita.datatree.xml.XmlSchema.*;
And then use those to generate a tree that represents the schema:
Tree schema = schema(element("foo"));
Tree document = tree("foo");
new XmlSchemaValidator(schema).validate(document);
You may also validate the schema itself against the XML Schema schema:
Tree schema = schema(element("foo"));
new XmlSchemaValidator(XmlSchema.XSD).validate(schema);
There are not yet helper methods for every XML Schema element and attribute. Maybe later. Create a pull request if you want to add something there.
We had to create lots of REST APIs for external consumption, but our customer did not allow us to use Clojure and JSON, but instead required us to use Java and XML, even producing XML Schemas. We tried to use JAXB for about two days. To protect our sanity and to save time, we created this library to have a succinct syntax for creating XML documents and schemas.
- Changed
XmlSchemaValidator
to have only instance methods, to avoid confusion with the order of parameters. The XML sources are stateful, so you must create a new instance of the validator for every document - Added
XmlVulnerabilities
utility for checking whether an XML document may contain an XXE or DoS attack
- Can validate the schema of schemas without the validator downloading the schemas for XML Schema and XML Namespaces from www.w3.org
tree
andmeta
will accept any instances and convert them automatically toString
usingtoString()
- Added
XmlGenerator.toByteArray()
- Removed
XmlDocumentGenerator.toXml()
- Renamed
XmlDocumentGenerator
toXmlGenerator
- Added
XmlGenerator.toInputStream()
- Added
XmlGenerator.toString()
andtoPrettyString()
- Added
XmlGenerator.toNamespaceAwareDocument()
XmlGenerator.toDocument()
won't anymore produce empty text nodes
tree()
will flatten alsoIterable
arguments- In
Tree.toString()
, show meta before text
null
in a tree's content does not anymore causeNullPointerException
, but will be ignored silently. We considernull
to be equivalent to an empty list, the same way as Clojure'snil
- Will throw an
IllegalArgumentException
if trying to put something other than String, Tree or Meta instances into a tree
XmlSchema
class with helper methods for generating XSD files
- A tree node may now contain both text and meta data
tree()
accepts arrays and collections of its arguments and will flatten them. This is useful when splitting tree construction into helper methods- Renamed
Tree.content()
toTree.text()
- Made the
Meta
class' constructor package-private to encourage using theTree.meta()
factory method
- Can create a tree using succinct Java syntax
- Can convert the tree into an XML document