我正在使用Groovy,我正在尝试将xml节点插入到使用XmlSlurper解析的xml文档中。 我设法在文档的末尾添加节点,但不是我真正需要的地方。
原始文件:
<xml-fragment xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config">
<ser:coreEntry isProxy="true" isEnabled="true" isTracingEnabled="false">
<ser:binding type="SOAP" isSoap12="false" xsi:type="con:SoapBindingType" xmlns:con="http://www.bea.com/wli/sb/services/bindings/config">
<con:wsdl ref="bus/src/main/osb/interfaces/apilink/ChargeServices"/>
<con:port>
<con:name>ChargeServicesPort</con:name>
<con:namespace>java:dk.tdc.apilink.logic.sessions.interfaces</con:namespace>
</con:port>
<con:selector type="SOAP body"/>
</ser:binding>
</ser:coreEntry>
</xml-fragment>
要添加的片段
def fragmentToAddXml = '''
<ser:security xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config">hello</ser:security>
'''
这是我正在使用的代码。
def root = new XmlSlurper().parseText(file.getText())
root.'core-entry'.appendNode( fragmentToAddXml )
def xmlBuilder = new groovy.xml.StreamingMarkupBuilder().bind{ mkp.yield root }
请注意,新节点应放在之前“ser:binding”节点。
结果应为:
<xml-fragment xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config">
<ser:coreEntry isProxy="true" isEnabled="true" isTracingEnabled="false">
<ser:security xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config">hello</ser:security>
<ser:binding type="SOAP" isSoap12="false" xsi:type="con:SoapBindingType" xmlns:con="http://www.bea.com/wli/sb/services/bindings/config">
<con:wsdl ref="bus/src/main/osb/interfaces/apilink/ChargeServices"/>
<con:port>
<con:name>ChargeServicesPort</con:name>
<con:namespace>java:dk.tdc.apilink.logic.sessions.interfaces</con:namespace>
</con:port>
<con:selector type="SOAP body"/>
</ser:binding>
</ser:coreEntry>
</xml-fragment>
由于
卢西亚诺
答案 0 :(得分:10)
给出xml(用于测试的字符串)
def xml = '''<xml-fragment xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config">
<ser:coreEntry isProxy="true" isEnabled="true" isTracingEnabled="false">
<ser:binding type="SOAP" isSoap12="false" xsi:type="con:SoapBindingType" xmlns:con="http://www.bea.com/wli/sb/services/bindings/config">
<con:wsdl ref="bus/src/main/osb/interfaces/apilink/ChargeServices"/>
<con:port>
<con:name>ChargeServicesPort</con:name>
<con:namespace>java:dk.tdc.apilink.logic.sessions.interfaces</con:namespace>
</con:port>
<con:selector type="SOAP body"/>
</ser:binding>
</ser:coreEntry>
</xml-fragment>'''
要添加的xml为:
def toadd = '''<ser:security xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config">
hello
</ser:security>'''
然后你可以解析它们(XmlSlurper
设置为通过第二个true
参数使用命名空间)
def root = new XmlSlurper( false, true ).parseText( xml )
fragmentToAdd = new XmlSlurper( false, true ).parseText( toadd )
附加xml以添加到data
节点(如您所希望的那样data
,而不是lastname
)
root.coreEntry.appendNode( fragmentToAdd )
然后打印出来:
String outxml = groovy.xml.XmlUtil.serialize( root )
println outxml
打印哪些:
<?xml version="1.0" encoding="UTF-8"?>
<xml-fragment>
<ser:coreEntry xmlns:ser="http://www.bea.com/wli/sb/services" isTracingEnabled="false" isProxy="true" isEnabled="true">
<ser:binding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" isSoap12="false" xsi:type="SOAP">
<con:wsdl xmlns:con="http://www.bea.com/wli/sb/services/bindings/config" ref="bus/src/main/osb/interfaces/apilink/ChargeServices"/>
<con:port xmlns:con="http://www.bea.com/wli/sb/services/bindings/config">
<con:name>ChargeServicesPort</con:name>
<con:namespace>java:dk.tdc.apilink.logic.sessions.interfaces</con:namespace>
</con:port>
<con:selector xmlns:con="http://www.bea.com/wli/sb/services/bindings/config" type="SOAP body"/>
</ser:binding>
<ser:security>
hello
</ser:security>
</ser:coreEntry>
</xml-fragment>
我认为这是正确的(没有按照我的意愿100%格式化,但是正确的); - )
如果订单很重要,您可以像这样使用XmlParser
:
def root = new XmlParser( false, true ).parseText( xml )
fragmentToAdd = new XmlParser( false, true ).parseText( toadd )
// Insert this new node at position 0 in the children of the first coreEntry node
root.find { it.name() == 'ser:coreEntry' }.children().add( 0, fragmentToAdd )
String outxml = groovy.xml.XmlUtil.serialize( root )
println outxml