[go: up one dir, main page]

0% found this document useful (0 votes)
71 views13 pages

Java Mapping Secrets Revealed - SAP Blogs

The document discusses parsing XML documents in Java mapping programs using SAX. It explains what parsing is, why it is needed for mapping, and provides details on the SAX parser and its core methods like startDocument, endDocument, startElement, endElement, and characters. It also includes a basic example Java mapping program that implements these SAX parser methods.

Uploaded by

Sujith Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views13 pages

Java Mapping Secrets Revealed - SAP Blogs

The document discusses parsing XML documents in Java mapping programs using SAX. It explains what parsing is, why it is needed for mapping, and provides details on the SAX parser and its core methods like startDocument, endDocument, startElement, endElement, and characters. It also includes a basic example Java mapping program that implements these SAX parser methods.

Uploaded by

Sujith Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

2/19/22, 12:11 PM Java Mapping secrets revealed | SAP Blogs

Community

Ask a Question Write a Blog Post Login

Former Member
July 27, 2008
| 5 minute read

Java Mapping secrets revealed


 12  0  1,055

Follow
 

 Like 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
 RSS Feed 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)
https://blogs.sap.com/2008/07/27/java-mapping-secrets-revealed/ 1/13
2/19/22, 12:11 PM Java Mapping secrets revealed | SAP Blogs

 *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 />}

https://blogs.sap.com/2008/07/27/java-mapping-secrets-revealed/ 2/13
2/19/22, 12:11 PM Java Mapping secrets revealed | SAP Blogs

 –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.*;

https://blogs.sap.com/2008/07/27/java-mapping-secrets-revealed/ 3/13
2/19/22, 12:11 PM Java Mapping secrets revealed | SAP Blogs

  //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)

https://blogs.sap.com/2008/07/27/java-mapping-secrets-revealed/ 4/13
2/19/22, 12:11 PM Java Mapping secrets revealed | SAP Blogs

                  {

                        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();

                            }

            }

https://blogs.sap.com/2008/07/27/java-mapping-secrets-revealed/ 5/13
2/19/22, 12:11 PM Java Mapping secrets revealed | SAP Blogs

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

Alert Moderator

Assigned Tags

Retagging required | sankararao bhatta

Similar Blog Posts 


Setting Custom Shared Secret value for Trusted Authentication in BI 4.0 HCM Processes & Forms: My Blogs
By
Prithviraj Shekhawat Apr 10, 2014 By
Christopher Solomon Feb 16, 2015

Passing Dynamic Signature (HMAC SHA256 algorithm) and Timestamp in


SAP PI REST Adapter Header Using Java Mapping
By
THAMARAISELVAN S Jan 13, 2022

Related Questions 
about mappings Java Mapping
By
Former Member Nov 06, 2008 By
Former Member Aug 22, 2008

java mapping
By
Former Member May 29, 2009

https://blogs.sap.com/2008/07/27/java-mapping-secrets-revealed/ 6/13
2/19/22, 12:11 PM Java Mapping secrets revealed | SAP Blogs

Join the Conversation 


SAP TechEd
Tune in for tech talk. Stay for inspiration. Upskill your future.

SAP BTP Learning Group


SAP Business Technology Platform Learning Journeys.

Coffee Corner
Join the new Coffee Corner Discussion Group.

12 Comments

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

Prateek Raj Srivastava


July 27, 2008 at 8:23 pm

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
https://blogs.sap.com/2008/07/27/java-mapping-secrets-revealed/ 7/13
2/19/22, 12:11 PM Java Mapping secrets revealed | SAP Blogs

Like 0 | Share

Former Member | Blog Post Author


July 28, 2008 at 2:59 am

Hi Prateek,

I will take your suggestion and update the same. Thanks for your feedback

thanks

sankar

Like 0 | Share

Former Member
July 28, 2008 at 3:58 am

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

Like 0 | Share

https://blogs.sap.com/2008/07/27/java-mapping-secrets-revealed/ 8/13
2/19/22, 12:11 PM Java Mapping secrets revealed | SAP Blogs

Former Member
July 28, 2008 at 4:02 am

Hi Prateek,

I misunderstood ur sentence.

sorry for the mistake.

Regards

Praveen

Like 0 | Share

Former Member
July 30, 2008 at 4:43 am

already one weblog posted on this topic with an example.

Please ref:

Testing and Debugging Java Mapping in Developer Studio

Using JAXP to both parse and emit XML in XI Java mapping programs

Like 0 | Share

Former Member | Blog Post Author


July 30, 2008 at 9:01 pm

Hi Prasad,

Thanks for posting ur comment.

https://blogs.sap.com/2008/07/27/java-mapping-secrets-revealed/ 9/13
2/19/22, 12:11 PM Java Mapping secrets revealed | SAP Blogs

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

Like 0 | Share

Former Member
August 7, 2008 at 6:19 am

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

Like 0 | Share

Former Member | Blog Post Author


August 8, 2008 at 3:19 am

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

Like 0 | Share

https://blogs.sap.com/2008/07/27/java-mapping-secrets-revealed/ 10/13
2/19/22, 12:11 PM Java Mapping secrets revealed | SAP Blogs

Former Member
September 22, 2008 at 7:48 pm

keep it man....we want one more blog related DOM parseing

Somu.

Like 0 | Share

Former Member
January 8, 2009 at 5:48 am

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

Like 0 | Share

Former Member
January 20, 2009 at 2:13 am

dear sankara rao bhatta, very gud blog.

https://blogs.sap.com/2008/07/27/java-mapping-secrets-revealed/ 11/13
2/19/22, 12:11 PM Java Mapping secrets revealed | SAP Blogs

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

Like 0 | Share
Former Member | Blog Post Author
January 20, 2009 at 5:16 am

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

Like 0 | Share

Find us on

Privacy Terms of Use

https://blogs.sap.com/2008/07/27/java-mapping-secrets-revealed/ 12/13
2/19/22, 12:11 PM Java Mapping secrets revealed | SAP Blogs

Legal Disclosure Copyright

Trademark Cookie Preferences

Newsletter Support

https://blogs.sap.com/2008/07/27/java-mapping-secrets-revealed/ 13/13

You might also like