cancel
Showing results for 
Search instead for 
Did you mean: 

Please provide Java Mapping example

brian_arthern2
Participant
0 Kudos

Hi all,

I am having difficulty finding a solid example of a Java Mapping program. The help on the SAP Portal is lacking in detail.

Could you please provide a complete example (including sample input, java program, and expected output).

My email is brian_arthern@yahoo.co.uk.

Many thanks

Brian

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Hi,

For java mapping,you need to have aii_map_api.jar on your XI server.

To program a Java mapping, you must define a Java class that implements the Java interface : com.sap.aii.mapping.api.StreamTransformation.

After creating ur java program,zip this file along with the class file, and import it into Xi as 'imported archives' under the mapping objects.,Leter this will be used in Interface mapping by selecting this imported file for java mapping.

See some links below , to get an idea..

/people/thorsten.nordholmsbirk/blog/2006/08/10/using-jaxp-to-both-parse-and-emit-xml-in-xi-java-mapping-programs

/people/stefan.grube/blog/2006/10/23/testing-and-debugging-java-mapping-in-developer-studio

http://help.sap.com/saphelp_nw04/helpdata/en/e2/e13fcd80fe47768df001a558ed10b6/frameset.htm

/people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-i

/people/prasad.ulagappan2/blog/2005/06/29/java-mapping-part-ii

https://www.sdn.sap.com/sdn/weblogs.sdn?blog=/pub/wlg/1947. [original link is broken] [original link is broken] [original link is broken] [original link is broken]

/people/ravikumar.allampallam/blog/2005/06/24/convert-any-flat-file-to-any-idoc-java-mapping

/people/michal.krawczyk2/blog/2005/02/25/simple-java-code-in-graphical-mapping--xi

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/xi/java%20proxies%...

https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/d046c490-0201-0010-84b6-9df523cb...

Regards

Chilla...

Former Member
0 Kudos

Please find the Programme and test data below ....You need to import the DOM4j.jar and AII_MAP_API.jar into the Java editor

package JavaMappingSample1;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.OutputKeys;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.apache.crimson.tree.TextNode;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.w3c.dom.Element;

import org.w3c.dom.Text;

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

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

import org.w3c.dom.*;

import org.xml.sax.SAXException;

import org.xml.sax.SAXParseException;

public class Average {

Document outdoc;

Document indoc;

String k;

private Node node;

public void execute(InputStream inputStream, OutputStream outputStream)

throws StreamTransformationException

{

System.out.println("Execution Started ... ");

try {

// request document

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder;

builder = factory.newDocumentBuilder();

indoc = builder.parse(inputStream);

//out document

factory = DocumentBuilderFactory.newInstance();

builder = factory.newDocumentBuilder();

outdoc = builder.newDocument();

} 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();

}

System.out.println("Stream Transformation Started ... ");

Element rootEle = outdoc.createElement("ns0:MT_Student_Receiver1");

rootEle.setAttribute("xmlns:ns0","http:
simplejavamapping_avg.com");

outdoc.appendChild(rootEle);

System.out.println("Root element added");

Element Marks = null ;

Element m1= null;

Element m2= null;

Element m3= null;

Element total = null;

Element avg = null;

Element StudentStore= null;

Element StudentInfo= null;

Element stnum = null;

Element stname= null;

NodeList nl_StudentStore = indoc.getElementsByTagName("StudentStore");

NodeList nl_StudentInfo = indoc.getElementsByTagName("StudentInfo");

NodeList nl_stnum = indoc.getElementsByTagName("stnum");

NodeList nl_stname = indoc.getElementsByTagName("stname");

NodeList nl_Marks = indoc.getElementsByTagName("Marks");

NodeList nl_m1 = indoc.getElementsByTagName("m1");

NodeList nl_m2 = indoc.getElementsByTagName("m2");

NodeList nl_m3 = indoc.getElementsByTagName("m3");

nl_Marks = indoc.getElementsByTagName("Marks");

try{

for(int item = 0;item<nl_StudentStore.getLength();item++)

{

if( "StudentStore".equals(nl_StudentStore.item(item).getNodeName()) )

{

System.out.println("entered into student store loop " );

StudentStore=outdoc.createElement("StudentStore");

//nl_StudentInfo = indoc.getElementsByTagName("StudentInfo");

rootEle.appendChild(StudentStore);

}//end of student store if

}//end of student store loop

for(int item1 = 0; item1 < nl_StudentInfo.getLength();item1++)

{

//System.out.println("nl_student Info : " + nl_StudentInfo.getLength());

System.out.println("nl_StudentInfo Name : " + nl_StudentInfo.item(item1).getNodeName());

if("StudentInfo".equals(nl_StudentInfo.item(item1).getNodeName()) )

{

System.out.println("nl_student Info : " + nl_StudentInfo.getLength());

//System.out.println("nl_StudentInfo Name : " + nl_StudentStore.item(item1).getNodeName());

StudentInfo=outdoc.createElement("StudentInfo");

StudentStore.appendChild(StudentInfo);

//begin of stname and number

for(int item = 0; item < nl_stnum.getLength();item++)

{

//System.out.println("nl_student Info : " + nl_StudentInfo.getLength());

System.out.println("nl_StudentInfo Name : " + nl_stnum.item(item).getNodeName());

if( "stnum".equals(nl_stnum.item(item).getNodeName()) )

{

if(item1== item)

{

System.out.println("ping : "+String.valueOf(nl_stnum.item(item).getLastChild()));

stnum = outdoc.createElement("stnum");

TextNode stnum_TextNode=(TextNode)outdoc.createTextNode(String.valueOf(nl_stnum.item(item).getLastChild()));

stnum.appendChild(stnum_TextNode);

StudentInfo.appendChild(stnum);

}

}//end of stnum if

}//end of stnum loop

for(int item = 0; item < nl_stname.getLength();item++)

{

//System.out.println("nl_student Info : " + nl_StudentInfo.getLength());

System.out.println("nl_StudentInfo Name : " + nl_stname.item(item).getNodeName());

if( "stname".equals(nl_stname.item(item).getNodeName()) )

{

if(item1== item)

{

System.out.println("ping : "+String.valueOf(nl_stname.item(item).getLastChild()));

stnum = outdoc.createElement("stname");

TextNode stnum_TextNode=(TextNode)outdoc.createTextNode(String.valueOf(nl_stname.item(item).getLastChild()));

stnum.appendChild(stnum_TextNode);

StudentInfo.appendChild(stnum);

}

}//end of stname if

}//end of stname loop

for(int item = 0;item<nl_Marks.getLength();item++)

{

if( "Marks".equals(nl_Marks.item(item).getNodeName()) )

{

if(item1== item)

{

System.out.println("entered into marks loop " );

Marks=outdoc.createElement("Marks");

StudentInfo.appendChild(Marks);

//add m1

for(int item_m1 = 0;item_m1<nl_m1.getLength();item_m1++)

{

if( "m1".equals(nl_m1.item(item_m1).getNodeName()) )

{

if(item==item_m1)

{

m1 = outdoc.createElement("m1");

TextNode m1_TextNode=(TextNode)outdoc.createTextNode(String.valueOf(nl_m1.item(item_m1).getLastChild()));

m1.appendChild(m1_TextNode);

Marks.appendChild(m1);

}

}

}

//end of m1 add

// add m2

for(int item_m2 = 0;item_m2<nl_m2.getLength();item_m2++)

{

if( "m2".equals(nl_m2.item(item_m2).getNodeName()) )

{

if(item==item_m2)

{

m2 = outdoc.createElement("m2");

TextNode m2_TextNode=(TextNode)outdoc.createTextNode(String.valueOf(nl_m2.item(item_m2).getLastChild()));

m2.appendChild(m2_TextNode);

Marks.appendChild(m2);

}

}

}

//end of m2 add

// add m3

for(int item_m3 = 0;item_m3<nl_m3.getLength();item_m3++)

{

if( "m3".equals(nl_m3.item(item_m3).getNodeName()) )

{

if(item==item_m3)

{

m3 = outdoc.createElement("m3");

TextNode m3_TextNode=(TextNode)outdoc.createTextNode(String.valueOf(nl_m3.item(item_m3).getLastChild()));

m3.appendChild(m3_TextNode);

Marks.appendChild(m3);

}

}

}

//end of m3 add

}// checkin index with parent marks

}//end of marks if

}//end of marks loop

}//end of student info if

}//end of student info loop

// cal total and avg

nl_StudentInfo = outdoc.getElementsByTagName("StudentInfo");

for (int item = 0; item < nl_m1.getLength(); item++)

{

int total1 = 0;

double avg1 = 0;

System.out.println("m1 : "item"::" + nl_m1.item(item).getChildNodes().item(0).toString() );

total1 = Integer.parseInt(nl_m1.item(item).getChildNodes().item(0).toString())Integer.parseInt(nl_m2.item(item).getChildNodes().item(0).toString())+Integer.parseInt(nl_m3.item(item).getChildNodes().item(0).toString());

avg1 = total1 / 3;

Node total_node = outdoc.createElement("total");

System.out.println("total1 : "+total1);

TextNode total_TextNode=(TextNode)outdoc.createTextNode(String.valueOf(total1));

total_node.appendChild(total_TextNode);

nl_StudentInfo.item(item).appendChild(total_node);

Node avg_node = outdoc.createElement("avg");

TextNode avg_TextNode=(TextNode)outdoc.createTextNode(String.valueOf(avg1));

avg_node.appendChild(avg_TextNode);

nl_StudentInfo.item(item).appendChild(avg_node);

// temp.item(item).appendChild(avg_node);

}

}//end of try

catch(Exception ex)

{

System.out.println("Exception in loop " + ex.toString());

}

System.out.println("SUCCESS");

try

{

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer();

transformer.setOutputProperty(OutputKeys.INDENT, "yes");

DOMSource source = new DOMSource(outdoc);

StreamResult result = new StreamResult(outputStream);

transformer.transform(source, result);

// transform.transform(new DOMSource(documentout), new StreamResult(out));

} catch (Exception e) {

throw new StreamTransformationException("Mapping failed", e);

}

}

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

{

Average tm=new Average();

try

{

System.out.println("First Line");

File fin = new File("D:/Test/st.xml");

FileInputStream fis = new FileInputStream(fin);

FileOutputStream fout = new FileOutputStream("D:/Test/stout.xml");

System.out.println("Second Line");

tm.execute(fis,fout);

System.out.println("Third Line");

}

catch(StreamTransformationException e)

{

System.out.println("Exception Third Line");

}

catch(FileNotFoundException fnfe)

{

System.out.println("fnfe"+fnfe.getMessage());

}

}*/

}

Test Data :

st.xml>>>>>>>>>>

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

<StudentStore>

<StudentInfo>

<stnum>1</stnum>

<stname>ddd</stname>

<Marks>

<m1>55</m1>

<m2>65</m2>

<m3>75</m3>

</Marks>

</StudentInfo>

<StudentInfo >

<stnum>2</stnum>

<stname >kiran</stname>

<Marks>

<m1>45</m1>

<m2>55</m2>

<m3>65</m3>

</Marks>

</StudentInfo>

<StudentInfo >

<stnum>3</stnum>

<stname >kiran</stname>

<Marks>

<m1>55</m1>

<m2>65</m2>

<m3>75</m3>

</Marks>

</StudentInfo>

</StudentStore>

stout.XML>>>>>>>>>

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

<ns0:MT_Student_Receiver1 xmlns:ns0="http:\simplejavamapping_avg.com">

<StudentStore>

<StudentInfo>

<stnum>1</stnum>

<stname>ddd</stname>

<Marks>

<m1>55</m1>

<m2>65</m2>

<m3>75</m3>

</Marks>

<total>195</total>

<avg>65.0</avg>

</StudentInfo>

<StudentInfo>

<stnum>2</stnum>

<stname>kiran</stname>

<Marks>

<m1>45</m1>

<m2>55</m2>

<m3>65</m3>

</Marks>

<total>165</total>

<avg>55.0</avg>

</StudentInfo>

<StudentInfo>

<stnum>3</stnum>

<stname>kiran</stname>

<Marks>

<m1>55</m1>

<m2>65</m2>

<m3>75</m3>

</Marks>

<total>195</total>

<avg>65.0</avg>

</StudentInfo>

</StudentStore>

</ns0:MT_Student_Receiver1>

Former Member
0 Kudos

Hi Kiran,

your example is very good, but I have a question for you: the input of my mapping is an XML Array and not a single XML. Ho can I manage this situation in my Java Mapping? How can I produce an XML Output for each input array cell that is an XML document?

Thanks!

Former Member
0 Kudos

Hi Brian,

Did you used to work for Axon ?

I have some examples for mapping I may be able to send you. I'll see if I can dig them out.

Why do you need to use Java - can you not use ABAP - it is much simpler.

Let me know.

Cheers

Colin.

Former Member
0 Kudos

Colin,

Do mark a copy to my id(saravana_kuppusamy@yahoo.com) as well. am trying some stuff on Java Mapping.

Thanks

brian_arthern2
Participant
0 Kudos

Hi Colin,

Yes I did, moved on to Fujitsu late last year. And yourself? Your name rings a bell but I can't put a face to it?

With regards to the Java mapping program - we are looking for the fastest method as the XI project I'm on will need to process around 3,000,000 record per day in a very tight processing window.

I have performance benchmark figures from SAP where they used the different mapping methods (they got throughput of 54,000,000 records per hour and determined the optimal message size to be 7MB). They also determined that ABAP mappings are the slowest by a very high factor, while Java Mapping programs are the fastest (providing you're a good Java programmer of course 🐵

If you could provide Java examples I'd really appreciate it.

Cheers

Brian

Former Member
0 Kudos

Dear Brian-

Have you considered using the XI Graphical Mapping tool for some of your mapping requirements? If additional functionality is required you could then call some user defined functions (java) from the graphical mapping for the trickier conversions. Just a thought...

regards, james

Former Member
0 Kudos

Hi Brian,

Essential java mapping works as follow.

With the method "execute(InputStream in, OutputStream out)", the InputStream contains an XML document in String format, and the OutputStream also must contain an XML document in String format.

You can parse the XML from InputStream and do whatever you want and construct another XML and assign it to the OutputStream.

Regards,

Bill