[antlr-interest] .java -> .xml -- lazy route

Andy Tripp antlr at jazillian.com
Fri Dec 1 13:22:09 PST 2006


Steve Lianoglou wrote:
>> Is there anything that would OUT-OF-THE-BOX let me do .java -> .xml
>> (Java AST to XML)?
>
> You mean serializing a java object to xml? I'm not sure what you're 
> really looking for, but if that's it, you can try this:
>
> http://xstream.codehaus.org/
>
> I've used it a couple of times and did what I expected it to w/ not 
> too much fuss.
>
> -steve
>
Sounds like he wants to store Java source in XML format, so using 
xstream to serialize the ASTs
that ANTLR produces (which represent the Java source) would work. You 
could also use castor
(www.castor.org) to save the ASTs in XML.

Or you could very simply walk the AST with code like:

	// A simple method to print out an AST as a tree:
	private static String SPACES = "                                              ";
	private static void showTree(AST t, int level) {
		if ( t==null ) return;
		System.err.print(SPACES.substring(0, level));
		System.err.println("text:" + t.getText() + " type=" + t.getType());
		AST child = t.getFirstChild();
		showTree(child, level+2);
		AST next = t.getNextSibling();
		showTree(next, level);
	}





More information about the antlr-interest mailing list