我的代码如下:
class GlobalInfo {
// multiple properties here
String name;
GlobalInfo parent;
static hasMany = [children: GlobalInfo]
}
class RegionalInfo extends GlobalInfo {
// multiple properties here
}
class BankInfo extends GlobalInfo {
// multiple properties here
}
一个RegionalInfo中可能有多个BankInfo。 BankInfo对象中可能有多个RegionalInfo父级。
例如,
BankInfo == Bank Of America
RegionalInfo == Dallas // 1st parent
RegionalInfo == Texas // Dallas's parent
RegionalInfo == MidWest // Texas's parent and final parent.
在上面的示例中,BankInfo对象的直接父级指向达拉斯,它的父级指向德克萨斯州,然后它的父级指向MidWest。
在某些情况下,可能只有一两个父母
例如,
BankInfo == University Credit Union
RegionalInfo == Houston
例如,来自最顶层父级的类层次结构视图将如下所示。这只是一个例子,我可以得到任何组合。
<MidWest>
<Independent Bank Of MidWest/>
<Midland Bank Of America/>
<Texas>
<Bank Of America>
<Austin>
<Supereme National Bank>
<Master Chanse Bank>
<Austin East>
<National Bank Of Austin>
<Federal Bank>
</Austin East>
</Austin>
</Texas>
<Kansas>
<Federal Credit Union>
<Kansas State Bank>
</Kansas>
</MidWest>
这些地方是RegionalInfo的对象,银行是BankInfo的对象。
这里的挑战是,正如您所看到的那样,在一个RegionalInfo对象中可能会有更多的RegionalInfo对象。
以下是我提出的代码。
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.root(name: "MidWest"){
bank(name: "Independent Bank Of MidWest")
bank(name: "Midland Bank Of America")
location(name: "Texas", expanded: true){
reader(name:"Bank Of America")
location(name: "Austin", expanded: true){
bank(name:"Supereme National Bank")
bank(name:"Master Chanse Bank")
location(name: "Austin East", expanded: true){
bank(name:"National Bank Of Austin")
bank(name:"Federal Bank")
}
}
}
location(name: "Kansas", expanded: true){
bank(name:"Federal Credit Union")
bank(name:"Kansas State Bank")
}
}
这里的问题是一切都是静态创建的。我想动态创建XML,因为类对象结构每次都会改变。
有可能吗?
创建此XML的目的是在TreeView插件中使用它来从最顶层的父级绘制类层次结构。在上面的示例中,最顶层的父级是 MidWest 。
已更新
为上面的代码生成的XML:
<root name='MidWest'>
<bank name='Independent Bank Of MidWest' />
<bank name='Midland Bank Of America' />
<location name='Texas' expanded='true'>
<bank name='Bank Of America' />
<location name='Austin' expanded='true'>
<bank name='Supereme National Bank' />
<bank name='Master Chanse Bank' />
<location name='Austin East' expanded='true'>
<bank name='National Bank Of Austin' />
<bank name='Federal Bank' />
</location>
</location>
</location>
<location name='Kansas' expanded='true'>
<bank name='Federal Credit Union' />
<bank name='Kansas State Bank' />
</location>
</root>
答案 0 :(得分:0)
像这样的东西(我通过移除hasMany改编了类,以便能够独立运行它们)?
import groovy.xml.MarkupBuilder
class GlobalInfo {
String name
GlobalInfo parent
List<GlobalInfo> children
}
class RegionalInfo extends GlobalInfo {
// multiple properties here
}
class BankInfo extends GlobalInfo {
// multiple properties here
}
String root(GlobalInfo root) {
def out = new StringWriter()
MarkupBuilder xml = new MarkupBuilder(out)
xml.root(name: root.name) {
root.children.each {
child(xml, it)
}
}
out.toString()
}
void child(MarkupBuilder xml, RegionalInfo info) {
xml.location(makeAttributes(info)) {
info.children?.each { child(xml, it) }
}
}
void child(MarkupBuilder xml, GlobalInfo info) {
throw new RuntimeException("GlobalInfo not expected here")
}
void child(MarkupBuilder xml, BankInfo info) {
xml.bank(makeAttributes(info))
}
protected LinkedHashMap<String, String> makeAttributes(GlobalInfo info) {
def attrs = [
name: info.name
]
if (info.children) {
attrs.expanded = true
}
attrs
}
def midwest = new GlobalInfo(
name: "MidWest",
children: [
new BankInfo(
name: 'Independent Bank Of MidWest'
),
new RegionalInfo(
name: 'Texas',
children: [
new BankInfo(
name: 'Bank Of America'
),
new RegionalInfo(
name: 'Austin',
children: [
new BankInfo(
name: 'National Bank Of Austin'
)
]
)
]
)
]
)
println root(midwest)
导致:
<root name='MidWest'>
<bank name='Independent Bank Of MidWest' />
<location name='Texas' expanded='true'>
<bank name='Bank Of America' />
<location name='Austin' expanded='true'>
<bank name='National Bank Of Austin' />
</location>
</location>
</root>