Details
-
Type:
Improvement
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: 1.1-beta-2
-
Component/s: XML Processing
-
Labels:None
-
Number of attachments :
Description
Closes a gap in GPathResult methods by providing a new "getBody()" method.
The method extracts the complete body of an XML tag into a closure wich can
then be inserted at another location in the XML builder.
The example below demonstrates how to use the getBody() method.
In this example the <sec> tag is replaced by a <t> tag.
Then the body of the <sec> tag is inserted into the <t> tag.
The expected result is: <doc><t>Hello<p>World</p></t></doc>
Existing methods of GPathResult ignore CDATA. In the example below
the "Hello" would be ignored.
def xmlOut = new StreamingMarkupBuilder()
def xmlInStr ="""
<doc><sec>Hello<p>World</p></sec></doc>
"""
GPathResult xmlIn = new XmlSlurper().parseText(xmlInStr)
xmlIn.sec.replaceNode{ node ->
t(){
delegate.mkp.yield node.getBody()
}
}
println xmlOut.bind{ mkp.yield xmlIn }
Below is the patch for GPathResult which resolves this issue.
Regards:
Frank
========================================================================
public Closure getBody() {
return new Closure(this.parent(),this) {
public void doCall(Object[] args) {
final GroovyObject delegate = (GroovyObject)getDelegate();
final GPathResult thisObject = (GPathResult)getThisObject();
Node node = (Node)thisObject.getAt(0);
List children = node.children();
for(int i=0; i<children.size(); i++){
;
Object child = children.get
delegate.getProperty("mkp");
if(child instanceof Node){
delegate.invokeMethod("yield", new Object[]{new NodeChild((Node)child, thisObject,"*",null)});
}
else{
delegate.invokeMethod("yield", new Object[]{child});
}
}
}
};
}