使用StreamingMarkupBuilder在标签内部生成CDATA [英] Generate CDATA inside of tag using StreamingMarkupBuilder

查看:370
本文介绍了使用StreamingMarkupBuilder在标签内部生成CDATA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要XML中的以下结构

 < DCS> 
<字节长度=29235><![CDATA [....]]>< / bytes>
< / DCS>

以下代码

  DCS {
bytes(length:$ {docBytes.size()},mkp.yieldUnescaped(<![CDATA [$ {docBytes}]]>))
}

但它会生成

 < DCS> 
<![CDATA [[....]]]>
<字节长度='135948'> groovy.xml.streamingmarkupsupport.StreamingMarkupWriter@32adca00< / bytes>
< / DCS>

如何生成所需的xml结构?
在此先感谢。

解决方案

您需要将 mkp.yieldUnescaped ,而不仅仅是 bytes 标记方法的第二个参数,即:

<$ p


$ b byte [] docBytes ='Tim Yates'

StreamingMarkupBuilder()。bind {
DCS {
bytes(length:docBytes.length){
mkp.yieldUnescaped<![CDATA [$ {docBytes.toList()。join('')}]]>
}
}
}

生成:

 < DCS><字节长度='9'><![CDATA [84 105 109 32 89 97 116 101 115 ]>< /字节>< / DCS> 

您可以在这里看到,我将每个字节编码为一个由空格分隔的字符串。你怎么做取决于你(我猜这取决于什么是解码字节); - )


I need following structure in XML

<DCS>
    <bytes length="29235"><![CDATA[....]]></bytes>
</DCS>

And have following code

DCS {
bytes(length: "${docBytes.size()}",mkp.yieldUnescaped("<![CDATA[${docBytes}]]>"))
}

But it generate

   <DCS>
    <![CDATA[[....]]]>
<bytes length='135948'>groovy.xml.streamingmarkupsupport.StreamingMarkupWriter@32adca00</bytes>
    </DCS>

How can I generate required xml structure ? Thanks in advance.

解决方案

You need to put your mkp.yieldUnescaped in a closure, not just as the second parameter to the bytes tag method, ie:

import groovy.xml.*

byte[] docBytes = 'Tim Yates'

new StreamingMarkupBuilder().bind {
  DCS {
    bytes( length:docBytes.length ) {
      mkp.yieldUnescaped "<![CDATA[${docBytes.toList().join(' ')}]]>"
    }
  }
}

Which generates:

<DCS><bytes length='9'><![CDATA[84 105 109 32 89 97 116 101 115]]></bytes></DCS>

You can see here, I've encoded each byte into a String separated by a space. How you do it is up to you (and I guess depends on what is going to be decoding the bytes) ;-)

这篇关于使用StreamingMarkupBuilder在标签内部生成CDATA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆