groovy - replace XmlSlurper tag with arbitrary XML -
I am trying to replace specific XmlSlurper tags with arbitrary XML strings. The best way is that I have been able to do this:
#! / Usr / bin / env groovy
Import groovy.xml.StreamingMarkupBuilder def page = New XmlSlurper (New org.cyberneko.html.parsers.SAXParser ()) parseText ("" ". & gt; & gt; & lt; / head & gt; & lt; body & gt; & lt; a ATTR1 = 'VAL1' & gt; asdf & lt; / a & gt; <2 /> gt; & lt; / body & gt; & lt; / html & gt; "" ".trim ()) import groovy.xml.xmlUtil DEF closed off = {Tie, node- & gt; If (node.name () == "REPLACEMEWITHXML") {bind.mkp.yieldUscaped "& lt; replacement xml> shortly before & lt; / replacexml & gt;" } Else {bind. "$ {Node.name ()}" (node attributes ()) {mkp.yield node.text () node. children (). Every child [child-> Close (Tie, child)}}}} println XmlUtil.serialize (new StreamingMarkupBuilder () bind {bind-> off (dam, page)})
However, only problem The text () element all starts catching the text nodes, and thus I am getting:
& Lt; Replacementxml & gt; Sometext & lt; / Replacementxml & gt; & Lt; / Body & gt; & Lt; / Html & gt;
Any thoughts / appreciate much.
Thank you! Misha
In addition to this, if I change the above "Griver" notation as follows, the Groovy compiler thinks that I try to use the $ {node.name ()} member of my test class I am here. Is there a way to specify that this is not even the actual builder object while passing? Thanks! :)
DIF close ends = {node-> If (node.name () == "REPLACEMEWITHXML") {mkp.yieldUscaped "& lt; replacement xml> Some time ago & Lt; / replacexml & gt; } Else {"$ {node.name ()}" (node attributes ()) {mkp.yield node.text () node. children (). Every child [child-> Close (child)}}}} println XmlUtil.serialize (New Streaming MarkupBuilder (). Tie {end (page)})
# / usr / bin / env groovy import groovy.xml.StreamingMarkupBuilder import groovy.xml.xmlUtil: itemprop =" text "> Here's exactly what I took Have come printSlurper = {page-> Println XmlUtil.serialize (new StreamingMarkupBuilder () bind {bind-> mkp.yield page}.)} Def saxParser = new org.cyberneko.html.parsers.SAXParser () saxParser.setFeature ('http: // xml. Org / saaks / features / namespaces', false) saxParser.setFeature ("http://cyberneko.org/html/features/balance-tags/document-fragment", true) def string = "test" def middleclosureHelper = {builder - & gt; Builder. "$ {String}" {mkp.yieldUscaped " XML " }} Middle of the middleclosure = {MiddlecloseHelp (Representative)}} DRF origin = new xmlresler (saxperres) .presitext ("" "" original "gt; gt; & lt; / middle & gt; & lt; / Original & gt; "" ") Original.depthFirst (). Find {it.name () == 'Middle'} .replaceNode {node-> Mkp.yield middleclosure} printSlurper claims the original (original) Original.depthFirst (). {It.name () == 'INNER'} == NULL DEF MODIFIED = Find new XML slipper (original) (new Streaming MarkupBuilder (.) Bind {Mkp.yield Origin}. ToString () has been modified You need to reload the error to find the modified dipeth first (). {It.name () == 'INNER'} = void
, but it works!
Misha
Comments
Post a Comment