Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Problem in java mapping

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.

replied

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

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question