Geertjan's Blog

  • July 24, 2008

Parsing XML with Groovy

Geertjan Wielenga
Product Manager
The web service looked at yesterday returns a Shakespeare speech in XML format based on the search string that is sent via Groovy from a Swing form created in Matisse. Groovy isn't only useful in interacting with the web service—parsing the returned XML is also a trivial activity with Groovy:

As shown yesterday, the Groovy method shown above is called from the button in the form, that you see deployed in the screenshot above. On the return of the XML, Groovy takes a tiny number of lines to break up the payload and then (without the "return" statement being necessary, as pointed out in the comments yesterday by Alex Tkachman) is returned to the Swing form.

Join the discussion

Comments ( 5 )
  • Tom Wheeler Thursday, July 24, 2008

    You ought to check out how easy it is to \*create\* XML with Groovy, as compared to doing the same thing using JAXP. You'll see it takes only a few lines of relatively clean code versus 50 lines of boilerplate mess.

  • mbien Friday, July 25, 2008

    the XML support in Groovy is pretty cool but JAXB is also strait forward.

    Object obj = null;

    try {

    JAXBContext jc = JAXBContext.newInstance("my.package", MyClass.class.getClassLoader());

    obj = jc.createUnmarshaller().unmarshal(file);

    } catch (JAXBException ex) {

    Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex);


    if(obj instanceof MyXMLObject == false)

    throw new IllegalArgumentException("something went wrong");

    MyXMLObject xmlObj = (MyXMLObject)obj;

  • 男性下着 Thursday, August 14, 2008
  • 勃起薬 Thursday, August 14, 2008

    勃起薬 勃起不全,勃起薬,勃起,勃起機能低下, インポ,精力剤,カマグラ,Max,MAX,マックス

  • 害虫駆除 Wednesday, August 27, 2008


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.