Nov

26

Groovy StAXBuilder

November 26, 2007 |

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"}
}}"""

Comments

Name (required)

Email (required)

Website

Speak your mind

News

Community