Groovy:动态创建XML以收集具有属性集合的对象 [英] Groovy: dynamically create XML for collection of objects with collections of properties

查看:142
本文介绍了Groovy:动态创建XML以收集具有属性集合的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些属性的字段集合。每个属性都是一个值或一个对象集合(null,one或many)。

b
$ b

到目前为止,我发现的所有示例都是静态的或将地图转换为xml。
在周期中向xml添加节点的正确方法是什么?

解决方案

你可以做这种事情,但没有任何输入的例子,所需的输出只是盲目的猜测:

  import groovy.xml。* 

def collection = [
[name:'tim',pets:['cat','dog'],age:null],
[name:'brenda',pets :null,age:32]
]

def process = {binding,element,name - >
if(element [name] instanceof Collection){
element [name] .each {n - >
绑定。$ name(n)
}
}
else if(element [name]){
binding。$ name(element [name ])



println XmlUtil.serialize(new StreamingMarkupBuilder()。with {builder - >
builder.bind {binding - >
data {
collection.each {e - >
item {
process(binding,e,'name')
process(binding,e,'pets'))
过程(绑定,e,'age')
}
}
}
}
})




 <?xml version =1.0encoding =UTF-8?>< data> 
< item>
< name> tim< / name>
<宠物>猫< /宠物>
<宠物>狗< /宠物>
< / item>
< item>
<名称>布伦达< /名称>
< age> 32< / age>
< / item>
< / data>






编辑



在下面的评论之后仍然不确定你有什么或者想要什么,但是这似乎满足了你的标准:

  import groovy.xml。* 

def process = {binding,element,name - >
if(element [name] instanceof Collection){
element [name] .each {n - >
绑定。$ name(n)
}
}
else if(element [name]){
binding。$ name(element [name ])
}
}

class表格{
列表字段
}

//创建一个新的Form对象
f = new Form(fields:[[name:'a',val:21],[name:'b'],[name:'c',val:[1,2],x:'field x ']])

//序列化为XML
println XmlUtil.serialize(new StreamingMarkupBuilder()。with {builder - >
builder.bind {binding - >
data {
f.fields.each {fields - >
item {
fields.each {name,value - >
process(binding,fields,姓名)
}
}
}
}
}
})

并打印:

 <?xml version =1.0encoding = UTF-8 >?<数据> 
< item>
<名称> a< /名称>
< val> 21< / val>
< / item>
< item>
<名称> b< /名称>
< / item>
< item>
< name> c< / name>
< val> 1< / val>
< val> 2< / val>
< x>字段x< / x>
< / item>
< / data>


I have a collection of fields with properties. Each property is a single value or a collection of objects (either null, one or many)

I need to create a tree like xml for that.

All examples I found so far either static or transform a map to xml. What is a proper way to add nodes to xml in a cycle?

解决方案

You can do this sort of thing, but without any examples of your input, and desired output it's all just blind guesswork:

import groovy.xml.*

def collection = [
  [ name:'tim', pets:['cat','dog'], age:null ],
  [ name:'brenda', pets:null, age:32 ]
]

def process = { binding, element, name ->
  if( element[ name ] instanceof Collection ) {
    element[ name ].each { n ->
      binding."$name"( n )
    }
  }
  else if( element[ name ] ) {
    binding."$name"( element[ name ] )
  }
}

println XmlUtil.serialize( new StreamingMarkupBuilder().with { builder ->
  builder.bind { binding ->
    data {
      collection.each { e ->
        item {
          process( binding, e, 'name' )
          process( binding, e, 'pets' )
          process( binding, e, 'age' )
        }
      }
    }
  }
} )

That prints:

<?xml version="1.0" encoding="UTF-8"?><data>
  <item>
    <name>tim</name>
    <pets>cat</pets>
    <pets>dog</pets>
  </item>
  <item>
    <name>brenda</name>
    <age>32</age>
  </item>
</data>


edit

Still not sure what you have or want after your comment below, but this seems to fullfil your criteria:

import groovy.xml.*

def process = { binding, element, name ->
  if( element[ name ] instanceof Collection ) {
    element[ name ].each { n ->
      binding."$name"( n )
    }
  }
  else if( element[ name ] ) {
    binding."$name"( element[ name ] )
  }
}

class Form {
  List fields  
}

// Create a new Form object
f = new Form( fields:[ [ name:'a', val:21 ], [ name:'b' ], [ name:'c', val:[ 1, 2 ], x:'field x' ] ] )

// Serialize it to XML
println XmlUtil.serialize( new StreamingMarkupBuilder().with { builder ->
  builder.bind { binding ->
    data {
      f.fields.each { fields ->
        item {
          fields.each { name, value ->
            process( binding, fields, name )
          }
        }
      }
    }
  }
} )

and prints:

<?xml version="1.0" encoding="UTF-8"?><data>
  <item>
    <name>a</name>
    <val>21</val>
  </item>
  <item>
    <name>b</name>
  </item>
  <item>
    <name>c</name>
    <val>1</val>
    <val>2</val>
    <x>field x</x>
  </item>
</data>

这篇关于Groovy:动态创建XML以收集具有属性集合的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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