我有一些代码可以为各种作业生成xml。它会生成很多标记,因此我使用StreamingMarkupBuilder。我能够成功地将整个xml生成为一个巨型xml文档。但是,这些作业有很多重复的部分,因此我想将其分解为一些方法,每个方法都会生成xml的某些部分。我尝试调用方法,但是只是将方法名称作为xml标记放置在输出中。这是我要生成的文档。它有更多部分,但是为了简洁起见,我将其删除。
<jobs xmnlns:"https://www.monitoring.silver.star.com/mngs/v1/ngmon">
<server>
<detail>crcscanner1</detail>
<ip> 220.10.12.72</ip>
<state>active</state>
</server>
<job>
<name>pcf-1</name>
<stage>1</stage>
<duration> 3 hours</duration>
<submittedByprocess-ngm</submittedByProcess>
<jobDetail>
<printing>
<card>10X6</card>
<graphicSize>4X6</graphicSize>
<scaleFactor> 1.5</scaleFactor>
<material>paper<material>
</printing>
</jobDetail>
</job>
<job>
<name>pcf-2</name>
<stage>2</stage>
<duration> 3 hours</duration>
<submittedBy>-ngm</submittedBy>
<jobDetail>
<printing>
<card>10X6</card>
<graphicSize>4X6</graphicSize>
<scaleFactor> 1.5</scaleFactor>
<material>paper<material>
</printing>
</jobDetail>
</job>
....
</jobs>
我使用此代码生成了运行良好的xml。但是,将所有xml代码都包含为一个巨型xml代码时,将很难阅读。我想将其分解为一些我将Server.java,Job.java类传递给方法的地方。这是xml,或者是其中的一部分,因为文档很大。
StreamingMakupBuilder builder = new StreamingMakupBuilder()
markupBuilder.encoding = 'UTF-8'
String orderXml = markupBuilder.bind {builder ->
mkp.xmlDeclaration()
jobs(xmlns:"https://www.monitoring.silver.star.com/mngs/v1/ngmon) {
server{
detail('crcscanner1')
ip('220.10.12.72')
state('ACTIVE')
}
job{
name('pcf-1')
stage('1')
duration('3 hours')
submittedBy('ngm</submittedByProcess')
}
jobDetail{
printing{
card('10X6')
graphicSize('4X10')
scaleFactor('1.5')
materia('paper')
}
buildJob(Job b) // This printed <buildJob></buildJob>
///... repeat the jobs
}
}
由于原始xml文档太大,因此创建了此示例作为测试。但是,它是基于尝试将对象传递给方法的。提供的此示例提供了我要做什么的想法。
import groovy.xml.*
def markupBuilder = new StreamingMarkupBuilder()
ManKing mowgli = new ManKing()
def xml = markupBuilder.bind { builder ->
client {
name 'mrhaki'
buildAddress builder, 'Main St.', 42, 'Ducktown'
buildManKing mowgli
}
}
def buildAddress(StreamingMarkupBuilder builder, streetName, number, city) {
builder.address {
houseNumber number
buildCity builder, city
}
}
def buildCity( StreamingMarkupBuilder builder, city) {
builder.city city
}
def buildManKing( StreamingMarkupBuilder builder, ManKing manKing){
builder.manKing{
name manKing.name
jungleFriend manKing.jungleFriend
}
}
class ManKing {
String name
String jungleFriend
}
这是包含该作品的结果。您可以看到使用了方法名和对象,因此建议的解决方案不适用于示例中传递对象的方法:
Result:
<client><name>mrhaki</name><buildAddress>Ducktown</buildAddress>
<buildManKing>ManKing@3eee45b2</buildManKing></client>
答案 0 :(得分:0)
最简单的方法是通过对每种数据类型使用单独的方法来分解XML创建。
def markupBuilder = new StreamingMarkupBuilder()
def xml = markupBuilder.bind { builder ->
client {
name 'mrhaki'
buildAddress builder, 'Main St.', 42, 'Ducktown'
}
}
def buildAddress(builder, streetName, number, city) {
builder.address {
houseNumber number
buildCity builder, city
}
}
def buildCity(builder, city) {
builder.city city
}
这些方法当然可以分散在多个类中。
完整的示例和说明为at groovy-goodness
答案 1 :(得分:0)
我找到了一个解决方案,可以传递整个对象并调用一组方法。我使用了委托,以在构建xml的createJobXml方法中获取闭包的范围。
def createJobXml(JobInfo jobInfo){ {
StreamingMarkupBuilder builder = new StreamingMarkupBuilder()
buildJob.delegate =delegate
buildServer.delegate =delegate
builder.bind{
jobs{
buildServer(jobInfo.server)
buildJob(jobInfo.job)
}
}
}
def buildJob = { Job j ->
job {
etc.
}
}
def buildServer = { Server s ->
server {
etc.
}
}