XML to Java SAX emitter compiler

H

Henri Sivonen

I am generating XML by emitting SAX events programmatically from Java.
However, there's a lot of boilerplate markup, so being able to generate
code from XML would be nice.

Is there a tool that would take an XML file like this

<?args org.xml.sax.ContentHandler ch, com.example.Foo foo ?>
<baz xmlns="http://example.com/fooml/">
<?code foo.bar(ch); ?>
</baz>

and would generate a Java class like this

public class GeneratedEmitter {
public static void emit(org.xml.sax.ContentHandler ch,
com.example.Foo foo) throws SAXException {
ch.startDocument();
ch.startElement("http://example.com/fooml/", "baz", "baz", new
AttributesImpl());
foo.bar(ch);
ch.endElement("http://example.com/fooml/", "baz", "baz");
ch.endDocument();
}
}

?
 
I

Ira Baxter

Henri Sivonen said:
I am generating XML by emitting SAX events programmatically from Java.
However, there's a lot of boilerplate markup, so being able to generate
code from XML would be nice.

Is there a tool that would take an XML file like this

<?args org.xml.sax.ContentHandler ch, com.example.Foo foo ?>
<baz xmlns="http://example.com/fooml/">
<?code foo.bar(ch); ?>
</baz>

and would generate a Java class like this

public class GeneratedEmitter {
public static void emit(org.xml.sax.ContentHandler ch,
com.example.Foo foo) throws SAXException {
ch.startDocument();
ch.startElement("http://example.com/fooml/", "baz", "baz", new
AttributesImpl());
foo.bar(ch);
ch.endElement("http://example.com/fooml/", "baz", "baz");
ch.endDocument();

Code generation from XML is a popular topic.

Since many XML documents represent trees,
doing a classic on-the-fly code generator by
tree walk is one way to do it. XSLT
can often be used to do this.

Other ways are to build ad-hoc code generators
in your favorite programming language by
making calls on a DOM to get at the XML data.

A tool that accepts source-to-source transformations,
that could do XML to Java generation directly
is the DMS Software Reengineering Toolkit.
See http://www.semanticdesigns.com/Products/DMS/DMSToolkit.html.
 
H

Henri Sivonen

Since many XML documents represent trees,
doing a classic on-the-fly code generator by
tree walk is one way to do it. XSLT
can often be used to do this.

Other ways are to build ad-hoc code generators
in your favorite programming language by
making calls on a DOM to get at the XML data.

A tool that accepts source-to-source transformations,
that could do XML to Java generation directly
is the DMS Software Reengineering Toolkit.
See http://www.semanticdesigns.com/Products/DMS/DMSToolkit.html.

Thanks. I already wrote a SAX compiler myself, but I have not gotten
around to releasing it yet. I think all those methods are overkills in
this case, because the mapping from SAX callback to generated code are
straightforward.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top