Groovy:将 XML 节点附加到现有 XML 文档
我正在使用 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>
谢谢
卢西亚诺
I'm using Groovy and I'm trying to insert an xml node into a xml document parsed with XmlSlurper.
I manage to add the node at the end of the document but not where I really need to.
Original doc:
<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>
Fragment to add
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>
'''
This is the code I'm using.
def root = new XmlSlurper().parseText(file.getText())
root.'core-entry'.appendNode( fragmentToAddXml )
def xmlBuilder = new groovy.xml.StreamingMarkupBuilder().bind{ mkp.yield root }
Please note that the new node should be placed before the "ser:binding" node.
The result should be:
<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>
Thanks
Luciano
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
给定 xml(用于测试的字符串)
和要添加的 xml:
然后您可以解析它们(将
XmlSlurper
设置为通过第二个true
使用命名空间参数)附加要添加到
data
节点的 xml(如您希望它位于data
中,而不是lastname
),出来:
然后打印 :
我认为这是正确的(不是按照我的意愿格式化 100%,但正确);-)
编辑
如果顺序很重要,您可以像这样使用
XmlParser
:Given the xml (in a string for testing)
And the xml you want to add as:
Then you can parse them both (with
XmlSlurper
set to use namespaces via the 2ndtrue
parameter)Append the xml to add to the
data
node (as you want it insidedata
, notlastname
)Then print it out:
Which prints:
Which I believe is correct (not formatted 100% as I'd like, but correct) ;-)
Edit
If order is important, you can use
XmlParser
like so: