我正在尝试使用groovy脚本创建XML文件。需要循环两件事,以便生成的XML包括用户传递的所有对象。
到目前为止,这是代码,第一个循环:
import groovy.xml.*
//map to loop
def workflows = [[ name: "A", file: "fileA" , objectName: "wf_A" , objectType: "workflow", sourceRepository: "DEV2"],
[ name: 'B' , file: 'fileB' , objectName: 'wf_B' , objectType: 'workflow', sourceRepository: 'DEV2']]
// def folderNameMap = [[ srcFolder: ["srcFolder1", "srcFolder2"], TgtFolder: ["TgtFolder1", "TgtFolder2"]],
// [srcFolder: ["srcFolder3"], TgtFolder: ["TgtFolder3"]]
// ]
def builder = new StreamingMarkupBuilder()
builder.encoding = 'UTF-8'
def xml = builder.bind {
mkp.xmlDeclaration()
'udm.DeploymentPackage'(version:'$BUILD_NUMBER', application: "informaticaApp"){
deployables {
workflows.each { item ->
'powercenter.PowercenterXml'(name:item.name, file:item.file){
scanPlaceholders{ mkp.yield(true) }
sourceRepository{ mkp.yield(item.sourceRepository) }
'folderNameMap' {
entry( key:"multifolder", "{{multifolderTST}}" ) // <- this is hard code and i want to remove this
}
'objectNames' {
value { mkp.yield(item.objectName) }
}
'objectTypes' {
value { mkp.yield(item.objectType) }
}
}
}
}
dependencyResolution{ mkp.yield('LATEST') }
undeployDependencies{ mkp.yield(false) }
}
}
println XmlUtil.serialize(xml)
生成的XML是:
<?xml version="1.0" encoding="UTF-8"?><udm.DeploymentPackage version="$BUILD_NUMBER" application="informaticaApp">
<deployables>
<powercenter.PowercenterXml name="A" file="fileA">
<scanPlaceholders>true</scanPlaceholders>
<sourceRepository>DEV2</sourceRepository>
<folderNameMap>
<entry key="multifolder">{{multifolderTST}}</entry>
</folderNameMap>
<objectNames>
<value>wf_A</value>
</objectNames>
<objectTypes>
<value>workflow</value>
</objectTypes>
</powercenter.PowercenterXml>
<powercenter.PowercenterXml name="B" file="fileB">
<scanPlaceholders>true</scanPlaceholders>
<sourceRepository>DEV2</sourceRepository>
<folderNameMap>
<entry key="multifolder">{{multifolderTST}}</entry>
</folderNameMap>
<objectNames>
<value>wf_B</value>
</objectNames>
<objectTypes>
<value>workflow</value>
</objectTypes>
</powercenter.PowercenterXml>
</deployables>
<dependencyResolution>LATEST</dependencyResolution>
<undeployDependencies>false</undeployDependencies>
</udm.DeploymentPackage>
这实现了声明为“工作流程”的地图的循环。 XML中还有另一个需要迭代的条目。脚本中的部分是
'folderNameMap' {
entry( key:"multifolder", "{{multifolderTST}}" ) // <- this is hard code and i want to remove this
}
如果为脚本提供了多个值,我需要迭代此部分并在生成的XML中创建新的行条目。像:
<folderNameMap>
<entry key="multifolder">{{multifolderTST}}</entry>
<entry key="multifolder2">{{multifolderTST2}}</entry>
<entry key="multifolder3">{{multifolderTST3}}</entry>
</folderNameMap>
如何定义第二个映射,以便生成的XML看起来像这样:( foldermap是一个映射。所以我会遇到只有一个srcFolder和一个tgtFolder的情况,或者有时候会有给出了多个srcFolder和TgtFolders。)
<?xml version="1.0" encoding="UTF-8"?><udm.DeploymentPackage version="$BUILD_NUMBER" application="informaticaApp">
<deployables>
<powercenter.PowercenterXml name="A" file="fileA">
<scanPlaceholders>true</scanPlaceholders>
<sourceRepository>DEV2</sourceRepository>
<folderNameMap>
<entry key="multifolder">{{multifolderTST}}</entry>
</folderNameMap>
<objectNames>
<value>wf_A</value>
</objectNames>
<objectTypes>
<value>workflow</value>
</objectTypes>
</powercenter.PowercenterXml>
<powercenter.PowercenterXml name="B" file="fileB">
<scanPlaceholders>true</scanPlaceholders>
<sourceRepository>DEV2</sourceRepository>
<folderNameMap>
<entry key="multifolder1">{{multifolderTST1}}</entry>
<entry key="multifolder2">{{multifolderTST2}}</entry>
<entry key="multifolder3">{{multifolderTST3}}</entry>
</folderNameMap>
<objectNames>
<value>wf_B</value>
</objectNames>
<objectTypes>
<value>workflow</value>
</objectTypes>
</powercenter.PowercenterXml>
</deployables>
<dependencyResolution>LATEST</dependencyResolution>
<undeployDependencies>false</undeployDependencies>
</udm.DeploymentPackage>
答案 0 :(得分:1)
所以,我正在黑暗中捅(因为我不是100%确定我知道你的问题是什么),但假设你的输入列表可以改为:
def workflows = [
[ name: 'A',
file: 'fileA',
objectName: 'wf_A',
objectType: 'workflow',
sourceRepository: 'DEV2',
folderNames: [ multifolder: '{{multifolderTST}}',
multifolder2: '{{multifolderTST2}}' ]],
[ name: 'B',
file: 'fileB',
objectName: 'wf_B',
objectType: 'workflow',
sourceRepository: 'DEV2',
folderNames: [ multifolder3: '{{multifolderTST3}}',
multifolder4: '{{multifolderTST4}}' ]]
]
然后,您可以这样做:
def builder = new StreamingMarkupBuilder()
builder.encoding = 'UTF-8'
def xml = builder.bind {
mkp.xmlDeclaration()
'udm.DeploymentPackage'(version:'$BUILD_NUMBER', application: "informaticaApp"){
deployables {
workflows.each { item ->
'powercenter.PowercenterXml'(name:item.name, file:item.file) {
scanPlaceholders(true)
sourceRepository(item.sourceRepository)
folderNameMap {
item.folderNames.each { name, value ->
entry(key:name, value)
}
}
objectNames {
value(item.objectName)
}
objectTypes {
value(item.objectType)
}
}
}
}
dependencyResolution('LATEST')
undeployDependencies(false)
}
}