jastrachan | 31 Mar 09:30 2004
Picon

XML parsing (was Re: MarkupBuilder methods)

On 31 Mar 2004, at 07:28, J. Matthew Pryor wrote:
> At the risk of killing people with my repetitiveness, SAX event  
> pipelining
> is what I am trying to achieve just now (well any efficient XML  
> oriented
> pipelining actually)

Firstly we can use any Java parsing technology (SAX, StAX, DOMish APIs  
etc) - plus if need be we can add extra helper methods to those APIs  
(e.g. W3C DOM) to make them more groovy & work neater with GPath.

We have our own simple little DOMish API in Groovy which is designed  
for easy XML parsing with dynamic typing & GPath. Its 1 simple class  
which supports either String names or QName names (for namespace aware  
documents), a Map of attributes and values being a String or a List of  
Stirng/Nodes.

http://cvs.groovy.codehaus.org/viewcvs.cgi/groovy/groovy-core/src/test/ 
groovy/gpath/NodeGPathTest.groovy?rev=HEAD&view=auto
http://cvs.groovy.codehaus.org/viewcvs.cgi/groovy/groovy-core/src/test/ 
groovy/util/XmlParserTest.groovy?rev=1.5&root=codehaus&view=auto

If you want to do some kind of event based processing of XML it might  
be worth writing a little helper class to wrap SAX / StAX allowing you  
to pass in a closure as a helper?

parser.eachElement { e |
     switch (e.localName) {
	case "cheese":
	    builder.cheddar(foo:123, bar:e.attributes.id)
	    break
	case "edam"
	    builder.unknown(x:123)
	}
     }
}

If its any help I hacked together an XSLT-like script for turning HTML  
into wiki markup using a similar technique - though using a DOMish  
rather than event based mechanism...
	
http://cvs.groovy.codehaus.org/viewcvs.cgi/groovy/groovy-core/src/test/ 
org/codehaus/groovy/wiki/Html2Wiki.groovy?rev=HEAD&view=auto

James
-------
http://radio.weblogs.com/0112098/

Gmane