Groovy条带中的XML分析属性为新行 [英] XML Parsing in Groovy strips attribute new lines

查看:118
本文介绍了Groovy条带中的XML分析属性为新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写从Web api检索XML的代码,然后使用Groovy解析该XML。不幸的是,当调用.text()时,似乎XmlParser和XmlSlurper for Groovy剥离了节点属性中的换行符。



如何获取文本包含新行的属性



示例代码:

  def xmltest ='''
< snippet>

这是第2行
这是第3行>
< lines count =10/>
< / preSnippet>
< / snippet>'''

def parsed = new XmlParser()。parseText(xmltest)
printlnParsed
parsed.preSnippet.each {前 - >
println pre.attribute('code');


$ b def slurped = new XmlSlurper()。parseText(xmltest)
printlnSlurped
slurped.children()。each {preSnip - >
println preSnip。@ code.text()
}

这是:

 解析
这是第1行这是第2行这是第3行
Slurped
这是第1行这是第2行这是第3行

好的,我能够转换文本之前,我解析它,然后重新转换后,一个拉:

  def newxml = xmltest.replaceAll(/ code =[^] * /){
return it.replaceAll(/ \ n /,〜#〜)
}
def parsed = new XmlParser()。parseText xmltest)
def code = pre.attribute('code')。replaceAll(〜#〜,\\\

不是我最喜欢的黑客,但它会一直工作,直到他们修复了他们的XML输出为止。

解决方案

新属性不支持属性 - 这是来自XML规范。他们结束了正常化,在这种情况下,这意味着它们被替换为空格字符。请参阅规范的这一部分: http://www.w3.org/TR/ REC-xml /#AVNormalize



我的团队遇到了这个问题,我们的解决方案是切换到使用元素而不是属性。


I'm writing code where I retrieve XML from a web api, then parse that XML using Groovy. Unfortunately, it seems that both XmlParser and XmlSlurper for Groovy strip newline characters from the attributes of nodes when .text() is called.

How can I get at the text of the attribute including the newlines?

Sample code:

def xmltest = '''
<snippet>
   <preSnippet att1="testatt1" code="This is line 1
   This is line 2
   This is line 3" >
      <lines count="10" />
   </preSnippet>
</snippet>'''

def parsed = new XmlParser().parseText( xmltest )
println "Parsed"
parsed.preSnippet.each { pre ->
       println pre.attribute('code');
}


def slurped = new XmlSlurper().parseText( xmltest )
println "Slurped"
slurped.children().each { preSnip ->
   println preSnip.@code.text()
   }

the output of which is:

Parsed
This is line 1    This is line 2    This is line 3
Slurped
This is line 1    This is line 2    This is line 3

Ok, I was able to convert the text before I parsed it, then re-convert after, a la:

def newxml = xmltest.replaceAll( /code="[^"]*/ ) {
   return it.replaceAll( /\n/, "~#~" )
}
def parsed = new XmlParser().parseText( xmltest )
def code = pre.attribute('code').replaceAll( "~#~", "\n" )

Not my favorite hack, but it'll do until they fix their XML output.

解决方案

New lines are not supported in attributes - this is from the XML specification. They end up 'normalised' which in this case, means they get replaced with a space character. See this section of the spec: http://www.w3.org/TR/REC-xml/#AVNormalize

My team had this problem and our solution was to switch to using elements rather than attributes.

这篇关于Groovy条带中的XML分析属性为新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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