How to add
after text in XML element in Groovy MarkupBuilder

半世苍凉 提交于 2019-12-12 01:58:01

问题


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?


回答1:


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>



回答2:


you can try to use xml cdata

<![CDATA[" and ends with "]]>


来源:https://stackoverflow.com/questions/31617642/how-to-add-xa-after-text-in-xml-element-in-groovy-markupbuilder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!