Parsing XML with Groovy

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.

Comments:

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.

Posted by Tom Wheeler on July 24, 2008 at 07:18 AM PDT #

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;

Posted by mbien on July 24, 2008 at 11:54 PM PDT #

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

Posted by 勃起薬 on August 14, 2008 at 04:46 PM PDT #

害虫駆除

Posted by 害虫駆除 on August 26, 2008 at 06:57 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today