xmlgroovymarkupbuilder

How to add 
 after text in XML element in Groovy MarkupBuilder


Basically we are changing the datasource XML for an InDesign project the way the old. We don't want to change the InDesign so I have to adapt the new XML from the new datasource (a SOAP webservice via Mule ESB). I use groovy to convert said XML, all was going until I faced this issue. The old XML in InDesign only works with this special character added to after every text: 
.

Here's an example of a working XML :

<?xml version='1.0' encoding='UTF-8'?>
<w_import_saisie_web><w_evenement><w_titre>My Title&#xA;</w_titre></w_evenement>
</w_import_saisie_web>

I am unable to add the special character in the Groovy script : Here's what I tried so far :

root = new XmlSlurper(false,false).parseText(payload)

    def xml = new StringWriter().with { w -> new groovy.xml.MarkupBuilder(w).with {
            mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")                       
                     "w_import_saisie_web"() {                      
                                "w_titre"( root.title + "&#xA;")    
                        }
                    }
        }
        w.toString()
    }

I also tried :

"w_titre"( root.title + '&#xA;') 
"w_titre"( root.title + '&amp'+'#xA;')  
"w_titre"( root.title + "&amp"+"#xA;") 

etc

I can print this with no problem

<?xml version='1.0' encoding='UTF-8'?>
<w_import_saisie_web><w_evenement><w_titre>My Title</w_titre></w_evenement>
</w_import_saisie_web>

But I can't seem to do this :

<?xml version='1.0' encoding='UTF-8'?>
<w_import_saisie_web><w_evenement><w_titre>My Title&#xA;</w_titre></w_evenement>
</w_import_saisie_web>

Can someone help me out?


Solution

  • You can use mkp.yieldUnescaped:

    println new StringWriter().with { w ->
        new groovy.xml.MarkupBuilder(w).with {
            escapeAttributes = false
            mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")                       
            w_import_saisie_web {                      
                w_titre {
                    mkp.yieldUnescaped 'woo&#xA;'
                }
            }
        }
        w.toString()
    }
    

    Which prints:

    <?xml version='1.0' encoding='utf-8'?>
    <w_import_saisie_web>
      <w_titre>woo&#xA;</w_titre>
    </w_import_saisie_web>
    

    And if you don't want to hardcode the &#xA;, you can use escapeControlCharacters from XmlUtil:

    import groovy.xml.XmlUtil
    
    println new StringWriter().with { w ->
        new groovy.xml.MarkupBuilder(w).with {
            escapeAttributes = false
            mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")                       
            w_import_saisie_web {                      
                w_titre {
                    mkp.yieldUnescaped XmlUtil.escapeControlCharacters('woo\n')
                }
            }
        }
        w.toString()
    }
    

    Which prints:

    <?xml version='1.0' encoding='utf-8'?>
    <w_import_saisie_web>
      <w_titre>woo&#10;</w_titre>
    </w_import_saisie_web>