Nov

26

Groovy StAXBuilder

November 26, 2007 | Leave a Comment

Description

Simple Groovy builder that uses StAX API to generate XML/JSON documents

Download

You can download the builder here

Source

Source code is currently available at http://code.google.com/p/dynawork/source.

Example

Here are a few examples taken from test cases

Creating XML using Woodstox

WstxOutputFactory factory = new WstxOutputFactory()
StringWriter writer = new StringWriter()
StaxBuilder builder = new StaxBuilder(
    factory.createXMLStreamWriter(writer)
)
builder.root1(a:5, b:7) {
    elem1('hello1')
    elem2('hello2')
    elem3(x:7)
}
assert writer,
"""<root1 a="5" b="7">
<elem1>hello1</elem1>
<elem2>hello2</elem2>
<elem3 x="7" />
</root1>"""

Creating JSON using Jettison

MappedNamespaceConvention con = new MappedNamespaceConvention();
StringWriter writer = new StringWriter()
MappedXMLStreamWriter mappedWriter
    = new MappedXMLStreamWriter(con, writer);
StaxBuilder builder = new StaxBuilder(mappedWriter)
builder.root1(a:5, b:7) {
    elem1('hello1')
    elem2('hello2')
    elem3(x:7)
}
assert writer,
"""{"root1":{"@a":"5","@b":"7",
"elem1":"hello1",
"elem2":"hello2",
"elem3":{"@x":"7"}
}}"""

Nov

13

Code samples for Chapter 10 demonstrating the Web Scripting Framework are available for download. Also, as this is the last chapter of the book you can download the archive containing code samples for all chapters from here. Now that I finally sorted out all these examples, it’s time to move on with new projects and resources related to the topic, so stay tuned.

News

Community