Neoh59
Neoh59

Reputation: 63

Groovy: print XML tag content "as is"

I have a Groovy script which parses a XML file using XmlParser. And I want to print a Node that contains XML/HTML code (see below).

The XML file look like this:

<root>
  <name>myname</name>
  <version>1.0</version>
  <entry>
    <locale>en</locale>
    <rules>
      <rule>
        <pattern>with.html.text</pattern>
        <replace-value><em>emphasis {0}</em> is ok</replace-value>
      </rule>
    </rules>
  </entry>
</root>

And I tried something like:

StringWriter sw = new StringWriter()
PrintWriter pw = new PrintWriter(sw)
XmlNodePrinter nodePrinter = new XmlNodePrinter(new IndentPrinter(pw,"", false))
root = new XmlParser().parseText(myxml)
allRules = root.'rules'.rule.findAll()
allRules.each { aRule ->
    nodePrinter.print(aRule.'replace-value'[0])
    println sw.toString()
}

I expect: <em>emphasis {0}</em> is ok

But it prints: <replace-value><em>emphasis {0}</em>is ok</replace-value>

How to avoid the <replace-value> tag and preserve the space between </em> and "is" ?

Thanks for your help

EDIT: Note that my goal is to transform the XML file into a CSV file. I updated the XML sample to be more precise. Rules are attached to a locale. So I need to be able, for each replace-value element, to retrieve for which key and which locale it must be applied.

Upvotes: 0

Views: 1386

Answers (2)

Rao
Rao

Reputation: 21379

Here data of a node itself has element tag. You can put that in a CDATA like below.

Just putting it into a groovy script:

def myXml='''
<root>
  <name>myname</name>
  <version>1.0</version>
  <entry>
    <locale>en</locale>
    <rules>
      <rule>
        <pattern>with.html.text</pattern>
        <replace-value><![CDATA[<em>emphasis {0}</em> is ok]]></replace-value>
      </rule>
    </rules>
  </entry>
</root>'''

def rootNode = new XmlSlurper().parseText(myXml)
println rootNode.entry.rules.rule.'replace-value'

Upvotes: 0

tim_yates
tim_yates

Reputation: 171144

You can do this, but it's not pretty:

def myxml = '''<root>
  <name>myname</name>
  <version>1.0</version>
  <entry>
    <locale>en</locale>
    <rules>
      <rule>
        <pattern>with.html.text</pattern>
        <replace-value><em>emphasis {0}</em> is ok</replace-value>
      </rule>
    </rules>
  </entry>
</root>'''

import groovy.xml.*

def root = new XmlParser().parseText( myxml )

root.entry.collectEntries { it ->
    [ locale:it.locale.text(),
      rule:it.rules.rule.'replace-value'[0].children().collect { node ->
          if( node instanceof String ) { node }
          else {
              new StringWriter().with { sw ->
                  new PrintWriter( sw ).with { pw ->
                      new XmlNodePrinter(new IndentPrinter(pw,"", false)).print( node )
                      sw.toString()
                  }
              }
          } 
      }.join() ]
}

It feels like there should be a prettier solution, but it currently eludes me :-(

Upvotes: 1

Related Questions