我的问题是使用Groovy并在使用MarkupBuilder和XMLSlurper时添加节点。
有人可以告诉我为什么当我将此代码运行到第一个println时,我的输出显示目标行为:
<target-line>0</target-line>
但是当我将它运行到第二个println时,我的目标行输出现在是:
<target-line>
<target-line>7</target-line>
</target-line>
// Complete code
import groovy.xml.*
public class AppendNodeExample {
//Constructor
public AppendNodeExample() {
}
def CreateNewMessage() {
def dataToProcess = '<response><applied-edits><line><id>0100</id>' +
'<flag><mnemonic>TRA</mnemonic><message>TRA Message ' +
'Info.</message></flag><flag><mnemonic>REB</mnemonic>' +
'<message>REB Message Info.</message><target-line>' +
'<target-line>7</target-line></target-line></flag><flag>'+
'<mnemonic>UNB</mnemonic><message>UNB Message Info.</message>'+
'</flag></line></applied-edits></response>'
def rootNode = new XmlSlurper().parseText(dataToProcess.replaceAll(':','-') )
def newXmlFile = new StringWriter()
def xmlCladNew = new groovy.xml.MarkupBuilder(newXmlFile)
xmlCladNew.response()
{
'applied-edits'()
{
rootNode.'applied-edits'.line.each
{ appliedEditsLine ->
line
{
id(appliedEditsLine.id)
appliedEditsLine.flag.each
{ nodeLineFlag ->
flag
{
mnemonic(nodeLineFlag.mnemonic)
message(nodeLineFlag.message)
'target-line'('0')
}
}
}
}
}
}
def rootNodeNew = new XmlSlurper().parseText(newXmlFile.toString())
// First println
println XmlUtil.serialize(rootNodeNew)
rootNodeNew.'applied-edits'.line.each
{ line ->
line.flag.each
{ lineFlag ->
if (lineFlag.mnemonic == 'REB') {
lineFlag.appendNode({'target-line'('7')})
} else {
// Remove the target-line node
lineFlag.'target-line'.replaceNode {}
}
}
}
XmlUtil.serialize(new StreamingMarkupBuilder().bind {mkp.yield rootNodeNew} )
// Second println
println XmlUtil.serialize(rootNode)
}
}
答案 0 :(得分:0)
重新格式化您的代码,使其可读并遵循任何类型的指南,以便更容易查看错误:
import groovy.xml.*
public class AppendNodeExample {
//Constructor
public AppendNodeExample() {
}
def createNewMessage() {
def dataToProcess = '''<response>
| <applied-edits>
| <line>
| <id>0100</id>
| <flag>
| <mnemonic>TRA</mnemonic>
| <message>TRA Message Info.</message>
| </flag>
| <flag>
| <mnemonic>REB</mnemonic>
| <message>REB Message Info.</message>
| <target-line>
| <target-line>7</target-line>
| </target-line>
| </flag>
| <flag>
| <mnemonic>UNB</mnemonic>
| <message>UNB Message Info.</message>
| </flag>
| </line>
| </applied-edits>
|</response>'''.stripMargin()
def rootNode = new XmlSlurper().parseText( dataToProcess )
def newXmlFile = new StringWriter()
def xmlCladNew = new groovy.xml.MarkupBuilder( newXmlFile )
xmlCladNew.response {
'applied-edits' {
rootNode.'applied-edits'.line.each { appliedEditsLine ->
line {
id( appliedEditsLine.id )
appliedEditsLine.flag.each { nodeLineFlag ->
flag {
mnemonic( nodeLineFlag.mnemonic )
message( nodeLineFlag.message )
'target-line'( '0' )
}
}
}
}
}
}
def rootNodeNew = new XmlSlurper().parseText( newXmlFile.toString() )
// First println
println XmlUtil.serialize( rootNodeNew )
rootNodeNew.'applied-edits'.line.each { line ->
line.flag.each { lineFlag ->
if( lineFlag.mnemonic == 'REB' ) {
lineFlag.appendNode { 'target-line'( '7' ) }
} else {
// Remove the target-line node
lineFlag.'target-line'.replaceNode {}
}
}
}
XmlUtil.serialize(new StreamingMarkupBuilder().bind {mkp.yield rootNodeNew} )
// Second println
println XmlUtil.serialize( rootNode )
}
}
new AppendNodeExample().createNewMessage()
在第二个println中,您打印出原始rootNode
,但您没有做任何事情,因此它只包含原始的<target-line>7</target-line>
节点。