在java或groovy中基于元素值合并两个xml消息[已关闭]

yshpjwxd  于 2022-11-21  发布在  Java
关注(0)|答案(2)|浏览(143)

已关闭。此问题需要更多focused。当前不接受答案。
**想要改进此问题吗?**更新问题,使其仅关注editing this post的一个问题。

5天前关闭。
Improve this question
我有两条消息,第一条消息如下。应该匹配的元素是root/items/data/subscription_id

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <id>AX8a8aa</id>
    <type>invoice</type>
    <billing_document_number>FRIN218</billing_document_number>
    <state>open</state>
    <account>
        <account_number>X00204</account_number>
        <custom_fields>
            <Country__c>France</Country__c>
            <Entity__c>Local Subsidiary</Entity__c>
        </custom_fields>
    </account>
    <items>
        <data>
            <amount>1161</amount>
            <service_end>2023-05-31</service_end>
            <service_start>2022-06-01</service_start>
            <subscription_id>8a8aa</subscription_id>
            <taxation_items>
                <tax_rate_name>TVA</tax_rate_name>
            </taxation_items>
        </data>
        <data>
            <amount>-104.25</amount>
            <service_end>2022-06-30</service_end>
            <service_start>2022-06-01</service_start>
            <subscription_id>8a8aa</subscription_id>
            <taxation_items>
                <tax_rate_name>TVA</tax_rate_name>
            </taxation_items>
        </data>
        <data>
            <amount>-104.25</amount>
            <service_end>2022-09-30</service_end>
            <service_start>2022-09-01</service_start>
            <subscription_id>91abd</subscription_id>
            <taxation_items>
                <tax_rate_name>TVA</tax_rate_name>
            </taxation_items>
        </data>
    </items>
</root>

第二条消息如下所示;它提供了关于订阅的更多细节。

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <id>8a8aa</id>
   <notes>test 1</notes>
   <CUP__c/>
</root>

我需要根据消息1合并两个有效负载:根目录-〉项目-〉数据-〉订阅标识=消息2:根-〉id
预期输出应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <id>AX8a8aa</id>
    <type>invoice</type>
    <billing_document_number>FRIN218</billing_document_number>
    <state>open</state>
    <account>
        <account_number>X00204</account_number>
        <custom_fields>
            <Country__c>France</Country__c>
            <Entity__c>Local Subsidiary</Entity__c>
            <SAP_ReceiverInternalID__c/>
        </custom_fields>
    </account>
    <items>
        <data>
            <amount>1161</amount>
            <service_end>2023-05-31</service_end>
            <service_start>2022-06-01</service_start>
            <subscription_id>8a8aa</subscription_id>
            <taxation_items>
                <tax_rate_name>TVA</tax_rate_name>
            </taxation_items>

            <id>8a8aa</id>
            <CUP__c/>
            <notes>test 1</notes>

        </data>

    </items>
</root>
wljmcqd8

wljmcqd81#

我试过这个,但它包含根元素,我不想这样

def toadd = '''<?xml version="1.0" encoding="UTF-8"?><root><id>8a8aa</id><notes>test 1</notes><CUP__c/></root>'''

def root = new XmlSlurper( false, true ).parseText( xml )
fragmentToAdd = new XmlSlurper( false, true ).parseText( toadd )

root.items.data.appendNode(fragmentToAdd)
//println groovy.xml.XmlUtil.serialize(root)
String outxml = groovy.xml.XmlUtil.serialize( root )

println outxml

回应是

<?xml version="1.0" encoding="UTF-8"?><root>
  <id>AX8a8aa</id>
  <type>invoice</type>
  <billing_document_number>FRIN218</billing_document_number>
  <state>open</state>
  <account>
    <account_number>X00204</account_number>
    <custom_fields>
      <Country__c>France</Country__c>
      <Entity__c>Local Subsidiary</Entity__c>
      <SAP_ReceiverInternalID__c/>
    </custom_fields>
  </account>
  <items>
    <data>
      <amount>1161</amount>
      <service_end>2023-05-31</service_end>
      <service_start>2022-06-01</service_start>
      <subscription_id>8a8aa</subscription_id>
      <taxation_items>
        <tax_rate_name>TVA</tax_rate_name>
      </taxation_items>
      <root>
        <id>8a8aa</id>
        <notes>test 1</notes>
        <CUP__c/>
      </root>
    </data>
  </items>
</root>
esyap4oy

esyap4oy2#

这一个工作正常:

def root = new XmlSlurper( false, true ).parseText( xml )
fragmentToAdd = new XmlSlurper( false, true ).parseText( toadd )

root.items.data.appendNode(fragmentToAdd.id) 
root.items.data.appendNode(fragmentToAdd.notes)
root.items.data.appendNode(fragmentToAdd.CUP__c)
root.items.data.appendNode(fragmentToAdd.PONumber__c)
root.items.data.appendNode(fragmentToAdd.CIG__c)
//println groovy.xml.XmlUtil.serialize(root)
String outxml = groovy.xml.XmlUtil.serialize( root )

println outxml

相关问题