XMLParser and XMLSlurper
Groovy provides XML parsing features which are great tools added on Java platform.
XMLParser:
- XML DOM contains functions for insert, delete, update, and traverse the XML tree nodes. But before manipulating such XML documents it must be loaded into the XML DOM object.
- XMLParser does this! It loads the XML document and converts it into XML DOM object which in turn can be access the functions like insert, delete and traverse XML trees. The code of XMLParser resides in groovy.util ()
- XMLParser can work on FILE objects and also on other input sources directly.
- This function returns groovy.util.node object.
XMLSlurper:
- Groovy have built in XMLSlurper which adds more functions and makes it more attractive that DOM parser.
- XMLSlurper have advantage over XMLParser that, XMLSlurper uses less memory than that of XMLParser.
- Returns GPathResult.
Similarities between XMLParser and XMLSlurper:
- Both reside in package groovy.util ()
- Both share same constructor parameters, the types share methods with same signature.
- The result of both XMLParser and XMLSlurper is either Node (XMLParser) or GPathResult (XMLSlurper).
- Object of type Node and GPathresult access both child element and attribute as they were properties of current object.
Common methods in groovy.util.node and GPathResult:
|
Node Method
|
Gpath Method
|
Shortcut Assigned
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
So, XMLParser and XMLSlurper can be used to obtain similar results under similar fashion. But if they produce similar results then there is no need of two separate classes.
Difference between XMLParser and XMLSlurper:
- There are similarities between XMLParser and XMLSlurper when used for simple reading but when we use them for advanced reading and when processing XML documents in other formats there are differences between two.
- XMLParser stores intermediate results after parsing documents. But on the other hand, XMLSlurper does not stores internal results after processing XML documents.
- The real, fundamental differences become apparent when processing the parsed information. That is when processing with direct in-place data manipulation and processing in a streaming scenario.