Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos

 

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”.

12 Comments