Home > Software design >  to merge two xml messages based an element value in java or groovy
to merge two xml messages based an element value in java or groovy

Time:11-15

I am having two messages, the first one is below. The element that should be matched is 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>

The second message looks like this; it gives more details on subscription.

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

I need to merge both payload based on Message 1 : root->items->data->subscription_id = Message 2 : root->id

Expected output should be like this :

<?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>



CodePudding user response:

I tried this one, but it includes root element, which I don't want

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

The response is with

<?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>

CodePudding user response:

This one is working properly :

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
  • Related