The following links may help navigating this page
No Feed Found

Groovy Rookie · 2005-09-21

Say, you were asked to parse a xml structure in order to remove childnodes from certain tag-name specified nodes. The xml structure is large, only automated approaches are feasible. Asume furthermore, the task is a one time shot. So, no need to generalize the classes written.

Asume now, that you have been parsing xml-structures in Java and SAX. You know what objects to construct and what methods to call. Clearly, this a task for Groovy the java based scripting shell. Since we do not need the exception handling fuzz, we do not want to write main methods, that only call it's parent object and one method, and we do not want to compile the mess every time we changed a tiny line, an easy to use scripting environment is what we need. As a second requirement, we want to use methods implmeneted in Java. Again, Groovy provides native access.

After all, we wanted to try out Groovy for months now, yet, we never had a matching task. Is Groovy easier to code then a equal java implementation? I'd say yes. Groovy has got some differences compared to java. For example, watch the use of the iterator 'it':

NodeList StateNodes =
      Node StateNode = (Node)it;

So, folks, here is my groovy script to parse a xml structure from a file, find all nodes named 'state', remove all branches below each 'state' node and write the result to a new xml file: parseAndRemove.groovy.

Commenting is closed for this article.