Adding Carriage Return or Line Feed IN xml FILE using JAVA Mapping through Command Prompt:
Problem Statements:
Adding Carriage Return or Line Feed after tag.
This problem can be solved by using XSLT and Java Mapping I am here define only through .
JAVA Mapping:
Here I take example of customer balance interface which Proxy to file scenario. And I am not using any java base tool like Eclipse for java or Netweaver Developer Studio. I am using simple java commands using command prompt to execute java file and creating jar file.This blog also explain basics of java mapping.
1. For Java Mapping first we need “aii_map_api.jar” jar files that can find from various ways
You can check below Wiki for that:
http://wiki.sdn.sap.com/wiki/display/XI/Where+to+get+the+libraries+for+XI+development
I used the last option define there
Copy files from the local cache directory:
C:\Users\BatraG(logon- user)\AppData\LocalLow\Sun\Java\Deployment\cache\javaws\http\DAM001(PISERVER) \P51000\DMrep\DMrepository
File name: RMaii_map_api.jar
You can rename that file to: aii_map_api.jar
- I create a file named: AddCRorLF.java with following code.
A simple code is written in java for add Carriage Return or Line Feed after tags.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.OutputKeys;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
public class AddCRorLF implements StreamTransformation{
public void execute(InputStream in, OutputStream out)
throws StreamTransformationException {
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(in);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer t = tFactory.newTransformer();
t.setOutputProperty(OutputKeys.INDENT, “yes”);
t.transform(new DOMSource(doc), new StreamResult(out));
}
catch(Exception e)
{
e.printStackTrace();
throw new StreamTransformationException(“Error During Mapping”, e);
}
}
public void setParameter(Map arg0) {
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- A time to compile the file.
Put both files in one folder as below:
Commands:
Cd\
Cd d:\gagan
Javac –classpath aii_map_api.jar AddCRorLF.java
After that class file will created.
Then use following command for creating jar file:
Jar cvf AddCRorLF.jar AddCRorLF.class AddCRorLF.java
Then you can see following file in the folder:
- Go to Repository part and I have created following Message mapping for Customer Balance:
Right click on imported archive and create new.
Give the name and click on create.
Click on import archive and then select “AddCRorLF.jar” then open.
Following files are uploaded.
Then Save And Activate.
Then Go to Interface Mapping that I have all ready created.
Earlier it is showing like that at test tab all result is coming in one line:
Now add our java mapping after message mapping.
Select our Class
You can see following java mapping added after message mapping
In Test tab.
I hope you enjoyed this……………….:)
Hi Gagan,
I appreciate your nice blog.
It is a nice blog for a beginner to learn java mapping in a simple way .
I hope more blogs from you.
Best Regards
Raja
http://help.sap.com/javadocs/nw04/current/pi/com/sap/aii/mapping/api/package-summary.html
is a useful link during java mapping.
Hi Gagan,
i found this blog very useful for learning java mapping.Good work.
Regards,
Muniyappan.
Hi Gagan,
I think this technique has been explained earlier in a wiki article.
http://wiki.sdn.sap.com/wiki/display/XI/Indent+XML+file
Regards
Anupam