Groovy从xml读取值 [英] Groovy read values from xml

查看:188
本文介绍了Groovy从xml读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例代码,我试图从XML文件中读取数据并操作这些值。当我在 http://ideone.com 上尝试时,这种方式效果很好。



在我的实际代码中,我调用了这样的内容:

  def xmlFile =path / to / xmlfile.xml 
def tcproj = new XmlParser()。parseText(getTemplate(xmlFile).toString())

但是,当我在实际代码中使用示例代码中指定的条件时,我会得到完全不同的结果。



在尝试调试时,我发现结果差异很大。当我试图做的时候,我的实际代码的结果是

println records.supported_versions.version.any {println it; it.toString()。matches(/ $ ver /)}



是这个

 版本[属性= {};值= [6.0.35.A]] 
版本[attributes = {};值= [7.0.25.B]]
false

当我做

  println records.supported_versions.version.toString()

我得到一个结果

  [version [attributes = {};值= [6.0.35.A]],版本[attributes = {};值= [7.0.25.B]]] 

有人可以帮我理解这里发生了什么,解决这个问题?

解决方案

您正在使用 XmlParser code> XmlSlurper 就像在这个例子中那样...



要使用XmlParser,您需要将代码更改为:

  class xmlWorker {
static def tcproj ='''< tcs>
< supported_versions>
< version> 6.0.35.A< / version>
< version> 7.0.25.B< / version>
< / supported_versions>
< / tcs>'''
}
def records = new XmlParser()。parseText(xmlWorker.tcproj)
$ b $ def ver =6.0.35 .A

println版本:+ ver

println records.supported_versions.version.any {
println it.text()
it ().text()。matches(/ $ {ver} /)
}

if(records.supported_versions.version.any {it.text()。matches(/ $ {ver} /)}){
printlnif
} else {
printlnelse
}


I have a Sample Code where I'm trying to read data from an xml file and manipulating the values. This worked perfectly fine when I tried it on http://ideone.com

In my actual code I'm calling something like this

def xmlFile = "path/to/xmlfile.xml"
def tcproj = new XmlParser().parseText( getTemplate(xmlFile).toString() )

But when I use the same if condition specified in the Sample Code in my actual code, I get a completely different result.

On trying to debug I found that the result varied a lot. The result on my actual code with this when I tried to do

println records.supported_versions.version.any { println it; it.toString().matches( /$ver/ ) }

was this

version[attributes={}; value=[6.0.35.A]]
version[attributes={}; value=[7.0.25.B]]
false

When I do

println records.supported_versions.version.toString()

I get a result

[version[attributes={}; value=[6.0.35.A]], version[attributes={}; value=[7.0.25.B]]]

Can someone help me understand what's happening here and how to solve this?

解决方案

You're using XmlParser instead of XmlSlurper as in that example...

To use XmlParser, you need to change the code to:

class xmlWorker {
  static def tcproj = '''<tcs>
                           <supported_versions>
                             <version>6.0.35.A</version>
                             <version>7.0.25.B</version>
                           </supported_versions>
                         </tcs>'''
}
def records = new XmlParser().parseText(xmlWorker.tcproj)

def ver = "6.0.35.A"

println "Version: " + ver

println records.supported_versions.version.any {
  println it.text()
  it.text().matches( /${ver}/ )
}

if( records.supported_versions.version.any { it.text().matches( /${ver}/ ) } ) {
  println "if"
} else {
  println "else"
}

这篇关于Groovy从xml读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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