cancel
Showing results for 
Search instead for 
Did you mean: 

JAVA mapping

Former Member
0 Kudos

Hi All,

I tried to implement a JAVA mapping(for my understanding). I used SAX parser. This is the input xml

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

<ns:MT_SRC xmlns:ns="urn:java:mapping">

<root>

<name>jai</name>

<id>234</id>

</root>

<root>

<name>jaishankar</name>

<id>5601</id>

</root>

</ns:MT_SRC>

and I wanted the output xml as

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

<ns:MT_TRG xmlns:ns="urn:java:mapping">

<Employee>

<Emp_Name>jai</Emp_Name>

<Emp_id>234</Emp_id>

</Employee>

<Employee>

<Emp_Name>jaishankar</Emp_Name>

<Emp_id>5601</Emp_id>

</Employee>

</ns:MT_TRG>

When I write the coding for <ns:MT_SRC> in start tag and end tag, I can see the output exactly as above(I used FileOutputStream to see the output xml). But when I create a jar and import it in XI, the tag <ns:MT_TRG> is not produced. Can any one say me why this behaviour?

Thanks & Regards,

Jai Shankar.

Accepted Solutions (1)

Accepted Solutions (1)

bhavesh_kantilal
Active Contributor
0 Kudos

Jai,

Can you give us the source code as well. That will be able to help us crack the problem.

Regards,

Bhavesh

bhavesh_kantilal
Active Contributor
0 Kudos

Jai,

In your case, you also need to write the logic for writing the attributes in the start element. Have you done this?

The namespace should be also created? Have you done this?

Regards,

Bhavesh

Former Member
0 Kudos

Bhavesh,

The code goes here....

package Pack1;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Map;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

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

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

public class TestClass extends DefaultHandler implements StreamTransformation {

public static void main(String args[]) {

try {

FileInputStream fi = new FileInputStream("d:/javaip.xml");

FileOutputStream fo = new FileOutputStream("d:/jai_op.xml");

TestClass imap = new TestClass();

imap.execute(fi, fo);

fi.close();

fo.close();

} catch (Exception e) {

e.printStackTrace();

}

}

private OutputStream arg1;

private Map map;

private int tag;

public void setParameter(Map arg0) {

map = arg0;

}

public void execute(InputStream arg0, OutputStream arg1)

throws StreamTransformationException {

DefaultHandler handler = this;

SAXParserFactory spf = SAXParserFactory.newInstance();

try {

SAXParser sp = spf.newSAXParser();

this.arg1 = arg1;

sp.parse(arg0, handler);

} catch (Throwable t) {

t.printStackTrace();

}

}

public void startDocument() throws SAXException {

write("<?xml version='1.0' encoding='UTF-8'?>");

// write ("<ns:MT_TRG xmlns:ns=\"urn:java:mapping\">");

}

private void write(String string) throws SAXException {

try{

arg1.write(string.getBytes());

arg1.flush();

}catch(Exception e)

{

e.printStackTrace();

}

}

public void endDocument() throws SAXException {

try {

// write("</ns:MT_TRG>");

arg1.flush();

} catch (IOException e) {

e.printStackTrace();

}

}

public void endElement(String uri, String localName, String qName)

throws SAXException {

// System.out.println("end tag :""uri :"uri"\tlname :"localName"\tqname :"qName);

String nam = localName;

if (nam.equals("")){

nam = qName;

}

if (nam.equals("root"))

{

tag = 1;

write("</Employee>");

}

if (nam.equals("name"))

{

tag = 2;

write("</Emp_Name>");

}

if (nam.equals("id"))

{

tag = 3;

write("</Emp_id>");

}

if (nam.equals("ns:MT_SRC")){

tag=5;

write ("</ns:MT_TRG>");

}

}

public void startElement(String uri,String localName,String qName,Attributes attributes)

throws SAXException {

// System.out.println("start :"+"uri :" uri"\tlname :"localName"\tqname :"+qName);

String nam = localName;

if (nam.equals("")){

nam = qName;

}

if (nam.equals("ns:MT_SRC")){

tag=5;

write ("<ns:MT_TRG xmlns:ns=""\"urn:java:mapping\""">");

}

else if (nam.equals("root"))

{

tag = 1;

write("<Employee>");

}

else if (nam.equals("name"))

{

tag = 2;

write("<Emp_Name>");

}

else if (nam.equals("id"))

{

tag = 3;

write("<Emp_id>");

}

else

tag = 4;

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

// {

// System.out.println("attributes :"+ attributes.getValue(i));

// }

}

public void characters(char[] ch, int start, int length)

throws SAXException {

String s = new String(ch,start,length);

// System.out.println("characters :"+ s);

switch (tag)

{

// case 1:

// write (s);

case 2:

// System.out.println("char :"+ s);

write (s);

break;

case 3:

write (s);

break;

}

}

}

Thanks & Regards,

Jai Shankar.

Former Member
0 Kudos

Bhavesh,

I have not handled attributes in my code. When <ns:MT_SRC> code is encountered I have written the <MT_TRG> tag. Should I not do this way? Can you tell me the correct way to handle attributes and name space?

Thanks & regrds,

Jai Shankar.

bhavesh_kantilal
Active Contributor
0 Kudos

Jai,

Try this :

public void startElement(String uri,String localName,String qName,Attributes attributes)
throws SAXException {

// System.out.println("start :"+"uri :" +uri+"tlname :"+localName+"tqname :"+qName);
String nam = localName;
if (nam.equals("")){
nam = qName;
}
if (nam.equals("<b>MT_SRC</b>")){
tag=5;
write ("<ns:MT_TRG xmlns:ns="+""urn:java:mapping""+">");
}

Regards,

Bhavesh

bhavesh_kantilal
Active Contributor
0 Kudos

Jai,

Start Element has 4 inputs :

<i>The qualified name, or qName. This is actually a combination of namespace information, if any, and the actual name of the element. The qName also includes the colon (: ) if there is one -- for example,

revised:response.

The namespace URI. an actual namespace is a URI of some sort and not the alias that gets added to an element or attribute name. For example,

http://www.nicholaschase.com/surveys/revised/ as opposed

to simply revised

The local name. This is the actual name of the element, such as question. If the document doesn't provide namespace information, the parser may not be able to determine which part of the qName is the

localName.

• Any attributes. The attributes for an element are actually passed as a collection of objects, as seen in the next panel.</i>

Regards,

Bhavesh

Former Member
0 Kudos

Bhavesh,

I tried with <MT_SRC> in start element, it does not write the MT_TRG tag.

Thanks & Regards,

Jai Shankar.

bhavesh_kantilal
Active Contributor
0 Kudos

Jai,

2 points,

1. It should be MT_SRC and not <MT_SRC> . Reason is that the < > are removed by SAX parser.

2. Use TRACE to the mapping trace. Use this code in the setParameter method,

//Declare the Trace object as a class variable.

public static AbstractTrace trace;

<i>trace =(AbstractTrace) param.get(StreamTransformationConstants.MAPPING_TRACE);</i>

Now, use <b>trace.addWarning("");</b> to add the trace statements as this will help in debugging.

And debug your code, by adding these trace statements.

You can see the trace under TRACE in MONI .

Regards,

Bhavesh

Former Member
0 Kudos

Bhavesh,

When I try with <MT_SRC> in start element the file output stream does not produce a <MT_TRG> tag but when I export this jar file, XI in run time produces <MT_TRG> tag. Am confused.

Thanks & Regards,

Jai Shankar.

Former Member
0 Kudos

Hi Jai,

Check whether the target Message Type in IR has the namespace same as that of the output of the Java Mapping. In ur case check whether the target MT in IR has the namespace "urn:java:mapping".

In ur output MT form java mapping also produce output with namespace.

Check this out.

Regards,

Sudharshan

Former Member
0 Kudos

Sudharshan,

Both the data types and message types for sorce and target is in the same name space urn:java:mapping.

Thanks & Regards,

Jai Shankar.

Former Member
0 Kudos

Jai,

Did u check the map in Interface mapping with the same input u used for the standalone program...

Also in SAX there is some option called namespaceaware.

Could be something on those lines???

Regards,

Sudharshan

Answers (0)