Groovy XmlUtil.serialze()在尝试序列化GPathResult时抛出'Prolog中不允许内容'错误 [英] Groovy XmlUtil.serialze() is throwing a 'Content is not allowed in prolog' error when trying to serialize a GPathResult

查看:377
本文介绍了Groovy XmlUtil.serialze()在尝试序列化GPathResult时抛出'Prolog中不允许内容'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Groovy的(1.7.3)XmlUtil.serialize(GPathResult)方法遇到了一个奇怪的问题。当我用GPathResult调用它时,它会抛出'内容在prolog中不允许'错误,但groovy.util.Node序列化正确。这是我想要的非常简单的Groovy脚本:

I've encountered a strange issue with Groovy's (1.7.3) XmlUtil.serialize( GPathResult ) method. It throws a 'Content is not allowed in prolog' error when I call it with a GPathResult, but groovy.util.Node is serializing just fine. Here is the very simple Groovy Script I am trying:

import groovy.xml.XmlUtil
import groovy.xml.StreamingMarkupBuilder

def xmlStr = """<?xml version="1.0" encoding="UTF-8"?><stuff>ver="1.0"><properties><foo>bar</foo></properties></stuff>"""

//to pretty print GPathResult -- NOT WORKING
def gpr = new XmlSlurper().parseText( xmlStr )
println XmlUtil.serialize( gpr )

println 'trying groovy.util.Node'
//to pretty print groovy.util.Node -- WORKS
def node = new XmlParser().parseText( xmlStr )
println( XmlUtil.serialize( node ) )  

这是我得到的输出:

This is the output I get:

[Fatal Error] :1:1: Content is not allowed in prolog.
ERROR:  'Content is not allowed in prolog.'
<?xml version="1.0" encoding="UTF-8"?>
trying groovy.util.Node
<?xml version="1.0" encoding="UTF-8"?>
<stuff ver="1.0">
 <properties>
  <foo>bar</foo>
 </properties>
</stuff>

我在Mac OS X上使用Groovy版本:1.7.3 JVM:1.6.0_20 Snow Leopard

I am using Groovy Version: 1.7.3 JVM: 1.6.0_20 on Mac OS X Snow Leopard

是否有其他人遇到这种情况?

Is anyone else experiencing this?

推荐答案

Groovy选择错误的 serialize 方法在 XmlUtil

Looks like a bug where Groovy is selecting the wrong serialize method to execute in XmlUtil

如果我们对它施加压力,它似乎适用于我(这是否适合您?)

If we hard-cast it, it seems to work for me (does it for you?)

println XmlUtil.serialize( (groovy.util.slurpersupport.GPathResult)gpr )

我已将它添加到JIRA中: http://jira.codehaus.org/browse/GROOVY-4285 所以希望,它将在未来的版本中得到修复,而且你不需要投射

I've added it to the JIRA here: http://jira.codehaus.org/browse/GROOVY-4285 So hopefully, it will be fixed in future releases, and you won't need the cast

JIRA现在声明修复已经进入主干,并将一般在1.7.4 +

[edit] The JIRA now states that a fix has gone into trunk, and will be generally available in 1.7.4+

这篇关于Groovy XmlUtil.serialze()在尝试序列化GPathResult时抛出'Prolog中不允许内容'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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