Skip to Content

 

When I decided to learn java mapping, I searched SDN for blogs on java mapping and found lot of blogs. Some of them are too technical and some of them given only coding and nothing else. As a developer, who did not have java background ,I found it very difficult to follow those blogs. So, I have decided to come up with this blog for beginners, which will give a basic understanding of how to do write a java mapping program.

  *What is parsing?* 

The literal meaning of parsing is “taking apart” or breaking down” something into smaller segments. Technically when we say parsing of an XML document, we are breaking the XML file into individual elements and reading the data contained in the elements ( body of the individual element)

 *Why do we need parsing in Java mapping:* 

A typical java mapping program takes the incoming XML message and transfers the data into target XML message structure. To do this the java program first needs to read what is there in the incoming XML message. This is done by parsing or breaking down the XML document into individual elements.

 

Java provides two ways of parsing the XML document

1)      SAX ( Simple API for XML )

2)      DOM ( Document Object Model)

Apart from the above two we can use JDOM and JAXP methods also.

The subject of this blog is SAX so I am going to concentrate more on SAX parser.

 

*Few things you should know about SAX parser*:

 

SAX parser is nothing but simple API for XML. In crude words set of classes used for reading the XML document

 

The five methods shown below are used when parsing the XML documents

— Called when the Parser starts parsing the Current XML File.

public void startDocument () throws SAXException<br />{<br />……<br />}

 –Called when the Parser Completes parsing the Current XML File.

public void endDocument () throws SAXException<br />{<br />……<br />}

 Called when the starting of the Element is reached. For Example if we have Tag
  called , then this method is called when
  Encountered while parsing the Current XML File. The AttributeList Parameter has
  the list of all Attributes declared for the Current Element in the XML File.

public void startElement (String name, AttributeList attrs) throws SAXException<br />{<br />……<br />}

  Called when the Ending of the current Element is reached. For example in the
  above explanation, this method is called when  tag is reached

public void endElement (String name) throws SAXException<br />{<br />……<br />}

  While Parsing the XML file, if extra characters like space or enter Character
  are encountered then this method is called. If you don’t want to do anything
  special with these characters, then you can normally leave this method blank.So this method can be used to read the values or body of the elements.

public void characters (char buf [], int offset, int len) throws SAXException<br />{<br />……<br />}<br /><br />Lets take a simple example and understand how this methods work. The incoming message has below format  * <strong>Understand the java Mapping program:</strong>   </p><p style=”margin: 0in 0in 0pt” class=”MsoNormal”>package jmapsax;</p><p style=”margin: 0in 0in 0pt” class=”MsoNormal”> </p><p style=”margin: 0in 0in 0pt” class=”MsoNormal”>import java.io.FileInputStream;</p><p style=”margin: 0in 0in 0pt” class=”MsoNormal”>import java.io.FileOutputStream;</p><p style=”margin: 0in 0in 0pt” class=”MsoNormal”>import com.sap.aii.mapping.api.StreamTransformation;</p><p style=”margin: 0in 0in 0pt” class=”MsoNormal”>import java.io.;

import java.util.Map;

import javax.xml.parsers.;</p><p style=”margin: 0in 0in 0pt” class=”MsoNormal”>import org.xml.sax.;

import org.xml.sax.helpers.*;

  //IMPORT statement imports the specified classes and its methods into the program

//Every java mapping program must implement the interface StreamTransformation and its methods execute() and setParameter() and extend the class  DefaultHandler.  

public class saxmapfinal1 extends DefaultHandler implements StreamTransformation{

 //Below is the declaration for  all the variables we are going to use in the subsequent methods.   

            private Map map;

            private OutputStream out;

 private boolean input1 = false;

            private boolean input2 = false;

private int number1;

private int number2;

private int addvalue;

private int mulvalue;

private int subvalue;

 

        String lineEnd =  System.getProperty(“line.separator”);

  //setParamater() method is used to store the mapping object in the variable “map” 

            public void setParameter (Map param)

                  {

                        map = param;

                  }

            public void execute (InputStream in, OutputStream out)

            throws com.sap.aii.mapping.api.StreamTransformationException

               {

                        DefaultHandler handler = this;

                        SAXParserFactory factory = SAXParserFactory.newInstance();

                        try {

                                    SAXParser saxParser = factory.newSAXParser();

                                    this.out = out;

                                   saxParser.parse(in, handler);

                             }

                        catch (Throwable t){

                                    t.printStackTrace();

                            }

            }

 //As seen above execute() method has two parameters “in” of type InputStream and “out” 0f type OutputStream. First we get a new instance of *SAXParserFactory *and from this one we create a new Instance of SAXParser. To the Parse Method of SaxParser, we pass two parameters, inputstream “in” and the class variable “handler”.Method “write” is a user defined method, which is used to write the string “s” to the outpurstream “out”.

To report this post you need to login first.

12 Comments

You must be Logged on to comment or reply to a post.

  1. Prateek Raj Srivastava
    Nice explaination!!

    U have mentioned that In java, parsing can be done using DOM and SAX. Just to make it clear, u may add that the usage of JDOM and JAXP are possible too.

    Regards,
    Prateek

    (0) 
  2. PRAVEEN KURNI
    Hi friend,

    very good effort to explain Java mapping in detail. Keepit up.

    Prateek its added in the blog..

    Apart from the above two we can use JDOM and JAXP methods also

    (0) 
    1. sankara rao bhatta Post author
      Hi Prasad,

      Thanks for posting ur comment.

      Yes you are right. There are several blogs in SDN on java mapping. My blog is intended for beginners who does not have knowledge in java mapping using SAX parser.

      thanks
      sanakr

      (0) 
  3. Srinivas Reddy
    I have read some webblogs regarding the java mapping but this is very clear and good for guys not having java back ground.

    I really appreciate your effort and expecting more blogs from you. keep it up.

    Thanks,

    Srini

    (0) 
    1. sankara rao bhatta Post author
      Hi Srinivas,

      Thanks for your comments. It is really encouraging  to see people appreciating the work.I will definetely come up with more blogs on XI in future.

      thanks,
      sankar

      (0) 
  4. Subhendu Sahu
    Hi,
    This is one of the blogs, I will rate the best in terms of the sincerity to make the subject comprehensible for all.No jargons and all the steps are clearly described.

    It will be appreciable, if you can write the version2 for a complecated structure.

    Regards,
    Subhendu

    (0) 
  5. navneet sumit
    dear sankara rao bhatta, very gud blog.

    But can u just clarify one doubt. here you said that we need to add bin folder path at environment variable, and for classpath need to give lib folder path..

    please tell me where i ll get these variable.

    thanks and regards,
    navneet

    (0) 
    1. sankara rao bhatta Post author
      hi navneet,

      thanks for your comments.

      right click on “My computer” icon on ur desktop and go to properties. in the properties select “advanced” tab, in that at the bottom u will find “environment variables”. if u click that u can see CLASSPATH entry. select that entry and click on Edit button. now u should be able to add an entry there.

      please let me know if u need any other info

      thanks
      sankar

      (0) 

Leave a Reply