cancel
Showing results for 
Search instead for 
Did you mean: 

Problem in java mapping

Former Member
0 Kudos

Hi,

I am new to XI.Am trying java mapping.

I have following message type as my input mesg:

1-mt_person

2- person

3- gender

3- fname

3- lname

I have following message type as my output mesg:

1- mt_employee

2- employee

3- title

3- emp_name

I have uploaded the following code:


import java.util.*;
import java.io.*;
import com.sap.aii.mapping.api.*;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class sp extends DefaultHandler implements StreamTransformation
{


    private Map map;
    private OutputStream out;

    private String flname = "";

    private int tag = 0;
    private final int person_tag = 1;
    private final int gender_tag = 2;
    private final int fname_tag = 3;
    private final int lname_tag = 4;




    private void writetofile(String str)
    {

         try
         {
            out.write(str.getBytes());

            out.flush(); 

         }

         catch(IOException e)
         {

//            throw new SAXException("I/O error",e);
         } 

    }


    public void characters(char s[],int start,int len) throws SAXException

    {
       String str = new String(s,start,len);

       switch(tag)
       {

         case gender_tag:

           if(str.equals("1"))
             writetofile("Mr.");
           else if(str.equals("2"))
             writetofile("Mrs.");
             
         break;



         case fname_tag:

            flname = str;      

         break;

         case lname_tag:

            flname = flname + str;
             
            writetofile(flname);
         break;

    }      


}


    public void setParameter(Map param)
    {

        map = param;
    }

    public void execute(InputStream in,OutputStream out) throws com.sap.aii.mapping.api.StreamTransformationException
    {

        DefaultHandler handler = this;        
        int i;
        SAXParserFactory spf = SAXParserFactory.newInstance();   
        try
        {
        SAXParser sp = spf.newSAXParser();
       
        this.out = out;

      
        sp.parse(in,handler);
       
        
        }
        }

        catch(Throwable t)
        {

            t.printStackTrace();
        }

    
    }

    public void startDocument() throws SAXException
    {

      // writetofile("<?xml version='1.0' encoding='utf-8' ?>");
    }     


    public void endDocument() throws SAXException
    {

       try
       {

          out.flush();
       }
       catch(IOException e)
       {

//           throw new SAXException("I/O error",e);
       }
    }


    public void startElement(String nsuri,String lname,String qname,Attributes atr) throws SAXException
    {

        String ele = lname;
  
       if("".equals(ele))
       {

           ele = qname;
       }   


     


      
       if(ele.equals("person"))
       {
  
           tag  = person_tag;  

           writetofile("<employee>");   
       }

     else if(ele.equals("gender"))
       {
  
           tag = gender_tag;     

           writetofile("<title>");   
       }

    else if(ele.equals("fname"))
       {
           tag = fname_tag;  
       
       }

    else if(ele.equals("lname"))
       {
           tag = lname_tag;
           writetofile("<emp_name>");          
       }

   }

    public void endElement(String nsuri,String lname,String qname) throws SAXException
    {

       String ele = lname;
  
       if("".equals(ele))
       {

           ele = qname;
       }   

   

       if(ele.equals("person"))
       {
  

           tag = 0; 
           writetofile("</employee>");   
       }

      else if(ele.equals("gender"))
       {
  
           tag = 0; 

           writetofile("</title>");   
       }
     else if(ele.equals("fname"))
       {
         
           tag = 0; 
           
       }


     else if(ele.equals("lname"))
       {
         
           tag = 0; 
           writetofile("</emp_name>");          
       }

   }

}

When I am testing the mapping the message that is displayed is XML not well-formed.

When I redirected the inputstream to a file I got the following data:

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

<p2:mt_person xmlns:p2="http://platinofile2file.com">

<person>

<gender>1</gender>

<fname>Sachin</fname>

<lname>Tendulkar</lname>

</person>

</p2:mt_person>

-


Please help me out.

Thnx.

Accepted Solutions (1)

Accepted Solutions (1)

stefan_grube
Active Contributor
0 Kudos

Hi Pravesh,

You have only considered the elements, but the SAX parser request to consider all tags whether they are nodes or elements.

The easiest way you can do:

    public void startDocument() throws SAXException{
           writetofile("<?xml version='1.0' encoding='utf-8' ?>");
           writetofile("<p2:mt_employee xmlns:p2="http://platinofile2file.com">");
    } 

    public void endDocument() throws SAXException    {
       try {
          writetofile("</p2:mt_employee>"); 
          out.flush();
       } catch(IOException e){
//           throw new SAXException("I/O error",e);
       }

or you code the root tag processing in the methods <i>startElement</i> and <i>endElement</i>.

Regards

Stefan

Answers (6)

Answers (6)

Former Member
0 Kudos

Hi Stefan,

Thank you for your help , its working now.

Thank you Bhavesh and all others for your inputs.

Former Member
0 Kudos

Hi Ananth,

I tried to redirect the outputstream to a file, but it was blank.

Former Member
0 Kudos

Hi Pravesh,

What Bhavesh told is correct, Now after uncommenting that code for generating XML version tag, it should work as you expected.

Regards,

Ananth

bhavesh_kantilal
Active Contributor
0 Kudos

Pravesh,

Also, use <b>equalsIgnoreCase</b> instead of equals.

Regards,

Bhavesh

Former Member
0 Kudos

Hi Bhavesh,

I tried with the modifications.

Still while testing the mapping same mssage is displayed:

"XML not well-formed"

I tried the code(with the streams being FileInputStream and FileOutputStream) externally with an input xml file of the following form:

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

<person>

<gender>1</gender>

<fname>sachin</fname>

<lname>tendulkar</lname>

</person>

when the code executed following were the contents of the genrated file:

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

<employee>

<title>Mr.</title>

<emp_name>sachintendulkar</emp_name>

</employee>

It works fine, but when I test it in XI it gives the error.

When I redirect the input stream in XI to a file I get the following data:

<p2:mt_person xmlns:p2="http://platinofile2file.com">

<person>

<gender>1</gender>

<fname>Sachin</fname>

<lname>Tendulkar</lname>

</person>

</p2:mt_person>

The mesg shows "XML not well-formed" , but the above data is well formed.

Please help.

Thnx.

Former Member
0 Kudos

Hi Pravesh,

Can u also redirect the output stream and give us what is getting generated after trasnformation.

regards,

Ananth

bhavesh_kantilal
Active Contributor
0 Kudos

Praevesh,

<b><?xml version='1.0' encoding='utf-8' ?></b>

This is not available and that is the reason for your error.

Make sure, that in your startDOcument() menthod, you hardcode and writethis.

Your earlier code had it COMMENTED. Just uncomment the same and it will work.

Regards,

Bhavesh

bhavesh_kantilal
Active Contributor
0 Kudos

Hi,

Add this to your START ELEMENT method and then append the attrib to your WRITETOFILE method so that even Attributes are sent to the target. This way, even Namespace will get sent,

<i>String attrib = "";

for (int att = 0; att < attrs.getLength(); att++)

{

attrib = attrib " " attrs.getQName(att) "=""\""+ attrs.getValue( attrs.getQName(att)) +"\"";

}

writetofile("< employee "+ attrib +">");</i>

Regards,

Bhavesh

bhavesh_kantilal
Active Contributor
0 Kudos

Hi,

Also, uncomment the

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

in startDocument.

Regards,

Bhavesh

moorthy
Active Contributor
0 Kudos

Hi Pravesh,

First try to run you java code as an independed java application. For this give the input xml.. For this you can go to message mapping and put your source structure on that and give the data values and then pick the source xml from there. And use this is as a input for the Java mapping...

Once it is successful in the independent application, then try to import this as Mapping jar file ..

Just go thru these threads-

Hope this helps,

Regards,

Moorthy

former_member187339
Active Contributor
0 Kudos

Hi,

When you create MT (both Sender and receiver), try giving blank as XML Namespace.

Regards

Suraj