简体   繁体   English

在两个标签之间解析groovy?

[英]Parsing in groovy between two tags?

I would like to parse this Gstring with groovy : 我想用groovy解析这个Gstring:

Format type : Key, Value. 格式类型:键,值。

   def txt = """ <Lane_Attributes>
                  ID,1
                  FovCount,600
                  FovCounted,598
                  ...
                  </Lane_Attributes> """

And get a map like : 并获得如下地图:

Map = [ID:1, FovCount:600, FovCounted:598]

How can I : 我怎么能够 :
- extract text between tag and ?, - 在标签和?之间提取文本,
- and convert to a map ? - 并转换为地图?

Try this: 试试这个:

def map = [:]
txt.replaceAll('<.+>', '').trim().eachLine { line ->
   def parts = line.split(',')
   map[parts[0].trim()] = parts[1].trim().toInteger()
}
   def txt = """ <Lane_Attributes>
                  ID,1
                  FovCount,600
                  FovCounted,598

                  </Lane_Attributes> """

def map = new HashMap()
def lane = new XmlParser().parseText(txt)

 def content =  lane.text()


content.eachLine {
 line -> 

def dual =  line.split(',')
def key = dual[0].trim()
def val = dual[1].trim() 
//println "key: ${key} value: ${val}"
map.put(key,val)

}

println "map contains " +  map.inspect() 

//Will print: map contains ["FovCounted":"598", "ID":"1", "FovCount":"600"] //将打印:地图包含[“FovCounted”:“598”,“ID”:“1”,“FovCount”:“600”]

your problem is the fact that the contents between the tags will need to keep the same format throughout or this code will break 你的问题是标签之间的内容需要保持相同的格式,否则这个代码就会中断

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM