xml - Using String Variables inside StreamingMarkupBuilder().bind{} in Groovy -


for below code :

def writer = new stringwriter() writer = new streamingmarkupbuilder().bind { project(){mytag('help me')} }   println(writer.tostring())   output be: <project><mytag>help me</mytag></project> 

now if have "mytag('help me')" in above code string var , want use shown below

def teststring = "mytag('help me')" def writer = new stringwriter() writer = new streamingmarkupbuilder().bind { project(){out<<teststring} } println(writer.tostring())   output getting is: mytag('help me')<project></project> expecting: <project><mytag>help me</mytag></project> 

am new groovy,anybody me proper implementation or find mistake above case ? please let me know if had use other class other streamingmarkupbuilder , xmlmarkupbuilder ? note in actual scenario me text variable contains lot more of child nodes nested .

you this; wrap node string { -> } , evaluate closure, set delegate , call closure:

import groovy.xml.*  def nodes = '''mytag( attr:'help me' ) {               |    anothertag( 'help me!' )               |}'''.stripmargin()  println xmlutil.serialize( new streamingmarkupbuilder().bind {     project {         c = eval.me( "{ -> $nodes }" )         c.delegate = delegate         c()     } } ) 

which prints:

<?xml version="1.0" encoding="utf-8"?><project>   <mytag attr="help me">     <anothertag>help me!</anothertag>   </mytag> </project> 

however, must careful, if nodes string comes outside system, can used execute code put in it.

if you're getting nodes in string, why not them write xml instead , save job? ;-)


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -