In this document I will be discussing the basic procedure of
converting PDF to XML using JAVA mapping. I tried to keep it as simple as I
can, so that every PI developer can understand it. Only pre-requisite is to
know basic java here.
Now we will come directly on the steps needed while creating
the JAVA mapping.
First of all, we have to create a project in SAP Net Weaver Developer
studio, click on file, and go to project as shown below:-
Create a JAVA project here.
Once the project is created then we have to create a package
as shown below:-
Now clicking on the java class, a java classes is created as
shown below:-
Libraries
to be used for the conversion of PDF to XML:-
Libraries used for
mapping purposes:-
Now we have to import
the library files in NWDS, for this we right click on the project(in this case
as Shubham) and go to “Properties” tab as shown below:-
After this click on the libraries tab, on the top of the
screen as shown below:-
After clicking on the Libraries tab click on the tab “ Add
External JAR’s” and then click on the OK tab as shown below:-
Doing this the entire required JAR files are added to the
project.
Now add the code in the Class file:
/*
* Created on Mar 24, 2014
*
* To change the template for this
generated file go to
*
Window>Preferences>Java>Code Generation>Code and
Comments
*/
packageShubham1;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.util.HashMap;
importjava.util.Map;
importcom.lowagie.text.pdf.PdfReader;
importcom.lowagie.text.pdf.parser.PdfTextExtractor;
importcom.sap.aii.mapping.api.AbstractTrace;
importcom.sap.aii.mapping.api.StreamTransformation;
/**
*@authorshubham.e.agarwal
*
*Tochangethetemplateforthisgeneratedtypecommentgoto
*Window>Preferences>Java>CodeGeneration>Codeand
Comments
*/
publicclassShubham2
implementsStreamTransformation{
private
Mapmap=null;
private
AbstractTracetrace
=null;
public
voidsetParameter(Map
arg0){
map
=arg0;// Store reference to the mapping parameters
if
(map==null){
this.map=newHashMap();
}
}
/*public
staticvoid
main(String[]args)
{//FOR EXTERNAL
STANDALONE TESTING
try
{
FileInputStream
fin=newFileInputStream
("C:\\test.pdf");//INPUT FILE
(PAYLOAD)
FileOutputStream
fout=newFileOutputStream
("C:/Users/Shubham.e.agarwal/My
Documents/pdfXML.xml");//OUTPUT FILE (PAYLOAD)
Shubham2
mapping=newShubham2();
mapping.execute(fin,
fout);
}
catch
(Exceptione1)
{
e1.printStackTrace();
}
}*/
public
voidexecute(InputStream
inputstream,OutputStream
outputstream){
try
{
String
msgType="MT_shubham";
//A dummy Message type, please change it as per
your requirement.
String
nameSpace="http://Shubham";
//A dummy namespace, please change it as per your
requirement.
String
str;
str="<?xml version=\"1.0\"
encoding=\"UTF-8\"?>\n"+
"<ns0:"+msgType+" "+"xmlns:ns0=\""+nameSpace+"\">";
str
=str+"\n<Record>";
PdfReader
reader=newPdfReader(inputstream);
PdfTextExtractor
pdf=newPdfTextExtractor(reader);
str
=str+pdf.getTextFromPage(1);
str=
str+"\n</Record>"+"\n</ns0:MT_shubham>";
byte
by[]=str.getBytes();
outputstream.write(by);
reader.close();
outputstream.close();
System.out.println(str);
}
catch(Exceptione){
e.printStackTrace();
}
}
}
Now we will design an integration scenario where a PDF will
be converted into XML document
INTEGRATION
SCENARIO:-
First of all we will create a data type as below:-
We will be using this data type for both sender and receiver.
MESSAGE TYPE:-
Same as above, we will be using this message type for both
sender and receiver.
INTERFACES:-
We will create the inbound and outbound interfaces using the
same message types
Outbound
Interface:-
Inbound
Interface:-
Now we will create imported archive where we will be using java
mapping but before that we need to export the Jar file as below:-
After exporting the JAR files we need to import them in the
imported archive so we Import the same and activate and save them as shown
below:-
Also import the external jar file whose libraries we are
using in our JAVA mapping code.
In my case I’m doing the PDF conversion so I used itext.JAR(IR_PDFUtility) is the Imported Archive name) which I need to import
into same namespace to avoid “JAVA class not instantiate” error. The screenshot of the same is
below:-
Now we will create the interface mapping in which we will be
using the java mapping created as below:-
ID OBJECTS:-
Now we will
be using the service BS_Shubham, as the sender and receiver both.
SENDER AGREEMENT:-
Now we will
be creating a communication channel which we will have to configure in sender
agreement
Now we will
be configuring this communication channel in the sender agreement and will
activate the same. Please find the screenshot of the same below:-
RECEIVER DETERMINATION:-
INTERFACE DETERMINATION:-
RECEIVER AGREEMENT:-
Now we will
be creating a receiver channel which we will be configuring in the receiver
agreement as shown below:-
- Now after
completing the IR and ID part , we will be testing the scenarioJ
Results:-
So finally the
results indicate that we were able to convert a PDF file into XML as shown
above.
Furthermore
we can split the XML using user defined function as per the requirement.
References:-
http://wiki.scn.sap.com/wiki/display/NWTech/XLS+to+XML+conversion+using+JAVA+Mapping+in+SAP+XI+7.0