cancel
Showing results for 
Search instead for 
Did you mean: 

Split multiple records for a single xml

Former Member
0 Kudos

Hi,

This is my input XML.

   `    <?xml version="1.0" encoding="UTF-8"?>

                        <queryResponse>

                                  <result>

                                            <done>true</done>

                                            <queryLocator xsi:nil="true" />

                                            <records xsi:type="sf:OpportunityLineItem">

                                                      <sf:Id>00kS0000005RwheIAC</sf:Id>

                                                             <sf:SAP_QuoteItemNumber__c>000001</sf:SAP_QuoteItemNumber__c>

                                            </records>

                                            <records xsi:type="sf:OpportunityLineItem">

                                                      <sf:Id>00kS0000005RwhfIA </sf:Id>

                                                      <sf:SAP_QuoteItemNumber__c>000002</sf:SAP_QuoteItemNumber__c>

                                            </records>

                                            <records xsi:type="sf:OpportunityLineItem">

                                                      <sf:Id>00kS0000005RwhgIAC</sf:Id>

                                                      <sf:SAP_QuoteItemNumber__c>000003</sf:SAP_QuoteItemNumber__c>

                                            </records>

                                            <records xsi:type="sf:OpportunityLineItem">

                                                      <sf:Id>00kS0000005RwhhIAC</sf:Id>

                                                      <sf:SAP_QuoteItemNumber__c>000004</sf:SAP_QuoteItemNumber__c>

                                            </records>

                                            <records xsi:type="sf:OpportunityLineItem">

                                                      <sf:Id>00kS0000005RwhiIAC</sf:Id>

                                                      <sf:SAP_QuoteItemNumber__c>000005</sf:SAP_QuoteItemNumber__c>

                                            </records>

                                            <records xsi:type="sf:OpportunityLineItem">

                                                      <sf:Id>00kS0000005RwhjIAC</sf:Id>

                                                      <sf:SAP_QuoteItemNumber__c>000006</sf:SAP_QuoteItemNumber__c>

                                            </records>

                                            <size>6</size>

                                  </result>

                        </queryResponse>`

I have to read this XML and split into 6 XMLper records.

so output should look like

1st XML

`   

<?xml version="1.0" encoding="UTF-8"?>

<records xsi:type="sf:OpportunityLineItem">

<sf:Id>00kS0000005RwheIAC</sf:Id>                                                  <sf:SAP_QuoteItemNumber__c>000001</sf:SAP_QuoteItemNumber__c>

</records>`

2nd xml

`   <?xml version="1.0" encoding="UTF-8"?>

<records xsi:type="sf:OpportunityLineItem">

<sf:Id> 00kS0000005RwhfIAC</sf:Id>                                                  <sf:SAP_QuoteItemNumber__c>000002</sf:SAP_QuoteItemNumber__c>

</records>`

3rd XML

`  <?xml version="1.0" encoding="UTF-8"?>

<records xsi:type="sf:OpportunityLineItem">

<sf:Id>00kS0000005RwhgIAC</sf:Id>

<sf:SAP_QuoteItemNumber__c>000003</sf:SAP_QuoteItemNumber__c>

</records>`

.....

....

so on upto 6th xml.

Please help , how to get it.

Accepted Solutions (1)

Accepted Solutions (1)

rajasekhar_reddy14
Active Contributor
0 Kudos

I dont think you need Java mapping for this because even though you are going to split record in to multiple times but the straucture is same but then content is difference ,only challening is building mapping logic.

Use multi mapping design and use combination of useOneAs many/split by value combination.

Former Member
0 Kudos

Hi Raja,

this is small part of my parent java mapping.  i am getting the XML dynamically and the number of fields and fieldnames in the record are dynamic and  i will know those only @ runtime.

So actualy i have to do this in Java mapping only.

Actualy i have a  SAX parser  written where i will feeding in each record. it is workign when i am feeding one record but not working for multiple record.So my idea is to split the record and send the data one by one and finaly construct the final output.

rajasekhar_reddy14
Active Contributor
0 Kudos

If structiure is dynamic then how you could define XML structure (data type/External definition)?

I think your source XML structure is same but it may contain multiple records , after that you have to split .

Former Member
0 Kudos

Hi Raja,

I am reading the XML in Java mapping.So it does not matter what my Structure is. It would have mattered if i use Graphical mapping.

rajasekhar_reddy14
Active Contributor
0 Kudos

gotcha, then really tough to build generic logic to handle dynamic XML structure i beleive.

Answers (2)

Answers (2)

ambrish_mishra
Active Contributor
0 Kudos

Hi,

You can use graphical mapping for this requirement. Should be achievable easily by multi-mapping.

Ambrish

Former Member
0 Kudos

Hi Ambarish, i cannot because my requirement is different - because

this is small part of my parent java mapping.  i am getting the XML dynamically and the number of fields and fieldnames in the record are dynamic and  i will know those only @ runtime.

So actualy i have to do this in Java mapping only.

Actualy i have a  SAX parser  written where i will feeding in each record. it is workign when i am feeding one record but not working for multiple record.So my idea is to split the record and send the data one by one and finaly construct the final output.

baskar_gopalakrishnan2
Active Contributor
0 Kudos

If your sender system is file content conversion then you can configure recorset per message value 1 to achieve this. BTW, what is your sender message structure?

Former Member
0 Kudos

Baskar... no the xml is dynamic and i  have to split this in java mapping.

Former Member
0 Kudos

Hello,

You want to create different XML files pertaining to each "record" node in the source ? If yes, then check below link:

http://wiki.sdn.sap.com/wiki/display/XI/Multi-Mapping+using+Java+Mapping

Thanks

Amit Srivastava

Former Member
0 Kudos

This is not helpful, the Java mapping is hardcoding the records which is not at all recommended.

Former Member
0 Kudos

Sam

I have just provided you the approach...BTW u never mentioned in your post that u need a exact java code to implement your logic (Atleast i can't able to see that...), so the foremost important thing while posting a thread on SDN is that you have to be precise - a basic rule ?

Now coming to the java logic, i don't see any complication in implementing this logic..Using a dom parser a simple java mapping can be cretaed.

Thanks

Amit Srivastava

Former Member
0 Kudos

Hi Amit,

Cool down

I am not looking for exact java code.  if u see the Wiki u have provided - there its hardcoding the data payload 1 , 2 and 3. There is nothing mentioned about dom or sax.

I am looking for the logic to split it.

Former Member
0 Kudos

Sam,

Don't worry i am not boiling...

Yes, indeed the wiki  is just showing how to implement multimapping concept using java mapping and using that u have to build your code, and no one on SDN will ever post a wiki/blogs for these specific and easy requirements...

Using above wiki i have implemented below code in 20 mins  so if u would have given a try i am sure you might have also implemented the same:

package test;

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

import java.io.FileOutputStream;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

public class Multimapping extends AbstractTransformation

{

   

public void transform(TransformationInput arg0, TransformationOutput arg1)

    throws StreamTransformationException {

     System.out.println ("1");

        try {

            this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream()) ;

        } catch (SAXException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (ParserConfigurationException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

    public void execute(InputStream in, OutputStream out)throws StreamTransformationException, ParserConfigurationException, SAXException, IOException

    {  

        // TODO Auto-generated method stub

        try

        {

            

            DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();                 

            DocumentBuilder builder =factory.newDocumentBuilder(); 

            /*input document in form of XML*/               

            Document doc = builder.parse(in);  

            /*document after parsing*/

            Document docOut=builder.newDocument();  

            TransformerFactory tf=TransformerFactory.newInstance();                

            Transformer transform=tf.newTransformer();

            //Document doc;

            Element root= docOut.createElement("Messages");  

            docOut.appendChild(root);

            root.setAttribute("xmlns:sm","http://sap.com/xi/XI/SplitAndMerge");

            System.out.println (root);

           

            Element root1= docOut.createElement("Messages");  

            root.appendChild(root1);

            NodeList units= doc.getElementsByTagName("records");

        

           

            for(int i=0;i<units.getLength();i++)

            {    

                System.out.println (units.getLength());

                 Node units1= doc.getElementsByTagName("records").item(i);

                root1.appendChild(docOut.adoptNode(units1.cloneNode(true)));

                System.out.println (root1.toString());

            }

            transform.transform(new DOMSource(docOut), new StreamResult(out));

        } catch (TransformerException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }  

    }

    public static void main(String args[]) throws StreamTransformationException

    {

        try

        {

   

            InputStream in=new FileInputStream ("C:\\Users\\Desktop\\Test.xml");

            FileOutputStream out=new FileOutputStream("C:\\Users\\Desktop\\Output.xml");

           

            Multimapping g=new Multimapping();

            g.execute(in, out);

        }

        catch(Exception e)

        {

            e.printStackTrace();

        }

    }

}

Thanks

Amit Srivastava