Skip to Content
avatar image
Former Member

Java Mapping

Hi Experts,

i am new of PI, i want to know how to add two numbers using java mapping

thanks

Thiru

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • avatar image
    Former Member
    Aug 24, 2012 at 05:54 AM

    you can refer below doc:

    http://www.riyaz.net/sap/xipi-java-mapping-demystified/415/

    and you search scn before posting basic Questions.

    Add comment
    10|10000 characters needed characters exceeded

  • Aug 24, 2012 at 07:25 AM

    Hi Thiru,

    the + operator does not add for strings, but for integers. If you have strings, convert them to integer.

    BTW: You can google such simple questions..

    Regards,

    Udo

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 24, 2012 at 05:22 PM

    Hi Thiru,

    As you said ur new to PI .I will give little info about why we r using Java Mapping.We usually go for Java Mapping , whn we are not able to achieve our requirement through graphical mapping or interest of developer , If we are using java mapping no need to create Message mapping. Import Jar as Imported Archive and activate Imported Archive. In operation mapping select Mapping program type as  Java Class ,activate Operation mapping and go test tab and test you java mapping. Below given is the sample code using SAX parser  . I given small info ,hope u understood.😉

    /***** this class is starting point in PI ****************/

    import java.io.InputStream;

    import java.io.OutputStream;

    import javax.xml.parsers.*;

    import org.xml.sax.Attributes;

    import org.xml.sax.ContentHandler;

    import org.xml.sax.Locator;

    import org.xml.sax.SAXException;

    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;

    public class Test extends AbstractTransformation

    {

        @Override

        public void transform(TransformationInput arg0,

    TransformationOutput arg1)

                throws StreamTransformationException {

            this.start(arg0.getInputPayload().getInputStream()

    ,arg1.getOutputPayload().getOutputStream());

           

            System.out.println("Java mapping started");

           

           

           

        }

        public void start(InputStream in, OutputStream out) {

            Test_1 hd = new Test_1(out);

            SAXParserFactory factory =

    SAXParserFactory.newInstance();

            try{

                SAXParser saxparser =

    factory.newSAXParser();

                saxparser.parse(in, hd);

            }

            catch(Exception e){e.printStackTrace();}

        }

       

    }

    /*****************SAX Parser pgm **************************/

    import java.io.OutputStream;

    import org.xml.sax.Attributes;

    import org.xml.sax.SAXException;

    import org.xml.sax.helpers.DefaultHandler;

    public class Test_1 extends DefaultHandler {

        String fullname="";

        private OutputStream out;

        String chars;

        Test_1(OutputStream out)

        {this.out= out;}

        public void print(String str) throws SAXException

        {

            try{

                out.write(str.getBytes());

                out.flush();

               

            }

            catch(Exception e){

                e.printStackTrace();

            }

        }

       

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

                throws SAXException {

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

            fullname = fullname+chars;

            System.out.println(fullname);

           

                //print(chars);

               

           

        }

        public void endDocument() throws SAXException {

            print("</MT_TEST>");

            // TODO Auto-generated method stub

           

        }

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

                throws SAXException{

            //super.endElement(uri, localName, name);

            if(name1.equals("A2"))

            {

                print(fullname);

                print("</A>");

               

            }

           

       

           

        }

        public void startDocument() throws SAXException {

            // TODO Auto-generated method stub

            print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

            print("<MT_TEST>");

        }

        public void startElement(String uri, String localName, String name,

                Attributes atts) throws SAXException {

            System.out.println("in start element");

            if(name.equals("A1"))

            {

               

                print("<A>");

            }

                   

           

        }

    }

    /************************Main method for stand alone testing ****************/

    package hari.javamapping;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    public class Main {

            public static void main(String[] args) throws IOException {

                try{

                Test tst = new Test();

                FileInputStream input= new

    FileInputStream("C:\\Testing\\src\\test.xml");

                FileOutputStream output= new

    FileOutputStream("C:\\Testing\\tgt\\test.xml");

                tst.start(input,output);

                }catch(Exception e){

                    e.printStackTrace();

                }

           

        }

    }

    /******************source data structure ******************/

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

    <MT_TEST>

    <A1>HARI</A1>

    <A2>KUMAR</A2>

    </MT_TEST>

    /********************************target datatype Structure ***********/

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

    <MT_TEST><A>HARIKUMAR</A></MT_TEST>

    Add comment
    10|10000 characters needed characters exceeded