cancel
Showing results for 
Search instead for 
Did you mean: 

Re: Adding carriage return or Line Feed at the end of XML File

Former Member
0 Kudos

Hi Gurus

Can you please let me know the easiest way to ADD Carriage return or line feed at the end of XML file. Please, review example below in red.

I did come across this article:

http://wiki.scn.sap.com/wiki/display/XI/How+to+append+Carriage+Return+in+the+end+of+each+tag+of+xml+...

However, does this UDF have to added to each field or the root node was unclear to me.

Interface Pattern:

ECC (ABAP Proxy) -> PI (Maps to XSD) -> .xml File placed on FTP server.

Please, let me know the best possible way to implement this.

Also, is there anything which can be done on the Proxy side in ECC itself.

Thank you so much,

Ritu

Example:

  • Current Output file:

<?xml version="1.0" encoding="ISO-8859-1" ?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

  • Output we want:


<?xml version="1.0" encoding="ISO-8859-1" ?>

<note>

<to>Tove</to>

<from>

Jani</from>

<heading>

Reminder</heading>

<body>

Don't forget me this weekend!</body>
</note>


Accepted Solutions (1)

Accepted Solutions (1)

gagandeep_batra
Active Contributor
0 Kudos

Hi Ritu,

Did you check the following blog that may help you to achieve the above requirement.

Regards

GB

Former Member
0 Kudos

Thanks!! Gagandeep

I did come across wanted to try UDF first - trying your code now.

I am going to copy everything between "+++++++++" to create my .java file as is and then execute your steps detailed on your blog.

I like the option to do without eclipse!!!

Thank you for your quick response will keep you posted,

Ritu

Former Member
0 Kudos

Hi Gagandeep

I received the following error when compiling in command prompt:

  • Used the JAR file from PI 7.1 :com.sap.xpi.ib.mapping.lib

"C:\Program Files\Java\jdk1.5.0_11\bin\javac" -cp com.sap.xpi.ib.mapping.lib.jar AddCRorLF.java

  • Also, I do have 2 more JAR files (com.sap.xi.mdt.api_api;com.sap.xpi.ib.mapping.lib;com.sap.xi.mapping.tool.lib_api): How do I write the below command below when I want compile multiple jar files with the .JAVA file I have.
  • How can I change the below command prompt to include all 3 jar files:

"C:\Program Files\Java\jdk1.5.0_11\bin\javac" -cp com.sap.xpi.ib.mapping.lib.jar AddCRorLF.java

Here is the error I am getting: Do I need to just include the above jar files -or-change anything in the code - please advice:

Here is some addl. info on the above error when execute with -Xlint:

com.sap.xpi.ib.mapping.lib.jar AddCRorLF.java -Xlint

AddCRorLF.java:18: warning: [deprecation] com.sap.aii.mapping.api.StreamTransfor

mation in com.sap.aii.mapping.api has been deprecated

import com.sap.aii.mapping.api.StreamTransformation;

                               ^

AddCRorLF.java:22: warning: [deprecation] com.sap.aii.mapping.api.StreamTransfor

mation in com.sap.aii.mapping.api has been deprecated

public class AddCRorLF implements StreamTransformation{

                                  ^

2 warnings

Thank you so much for your valuable time & advice,

Ritu

praveen_sutra
Active Contributor
0 Kudos

hi Ritu,

the screenshot shows they are warning not errors.

That should not have any problem in executing the program.

And if warning is bothering it is because you must be using execute function which is deprecated and hence you should transform function instead.

the above link can just help you with usage of transform. You can use same structure and change the code accordingly.

hope that helps.

thanks and regards,

Praveen T

Former Member
0 Kudos

Thanks!! Praveen - I will try using the .class . java I have currently - and see if it works.

Also, can you please let me know how to change the command to compile using 3 jar files:

Currently I am using the command:

  • "C:\Program Files\Java\jdk1.5.0_11\bin\javac" -cp com.sap.xpi.ib.mapping.lib.jar AddCRorLF.java
  • How do I include all 3 jar files com.sap.xpi.ib.mapping.lib.jar & com.sap.xpi.ib.mapping.lib & com.sap.xi.mapping.tool.lib_api.jar

Thanks your for your timely help!

Ritu

praveen_sutra
Active Contributor
0 Kudos

hi Ritu,

there are two ways,

1) copy all the jars in C:\Program Files\Java\jdk1.7.0_17\jre\lib\ext  some similar kind of folder in your case and simply compile it without any class path. it should work.


2) java -cp <path>\TOOLS.jar;<path>\jar2.jar;<path>\jar3.jar [javafile-name]


any of the above option should work.


thanks and regards,

Praveen T

Former Member
0 Kudos

Thanks!! Praveen

We were able to follow your steps and get the XML in the format needed.

Thanks again,

Ritu

Answers (2)

Answers (2)

RaghuVamseedhar
Active Contributor
0 Kudos

Ritu,

Please let us know, what is the need to Indent / PrettyPrint XML?

If target system want an Indent XML: -

Simple solution to Indent XML in SAP PI is XSLT mapping. Please check this.

<xsl:output omit-xml-declaration="yes" indent="yes"/>

If target system user want an Indent XML: -

Please check this wiki.

Harish
Active Contributor
0 Kudos

Hi Ritu,

The UDF is adding carriage return at each node. If you want to add only to at the end then modify

    //write your code here
String res = "";
int flag = 0;
for(int i = 0;i<xmlString.length();i++)
{

  if(xmlString.charAt  =='Last tag of XML')
  {
   flag = 0;
   res = res +xmlString.charAt  + "\n";
  }
  else
  {
   res = res +xmlString.charAt  ;
  }
}

return res;

}

Former Member
0 Kudos

Thank you for your quick response!! Harish

Being very week in Java: Can you please tell me wat the input & output parameters would be, and if will apply to single value or all values of queue.

Also, wat would I connect it to

1.If I want it for every tag.

2.If I want it for only last tag (in a record set).

Always very grateful for your prompt response & help!!!

Thanks again,

Ritu

Former Member
0 Kudos

1.If I want it for every tag. --


you have to choose as "return xml" and pass as input parameter,

Harish
Active Contributor
0 Kudos

Hi Ritu,

The UDF is value UDF because its use return.

The input paramter is xmlString. Just add as string in UDF tab.And as reply by Ram you need to pass the root node as XML to the UDF

public String AddNewLineinEndofXMLTag(String xmlString,Container container)

Former Member
0 Kudos

Thanks for your quick response!! Harish, Ramkumar

Can you please help with the syntax error in the code: as the activation change list cancelled with error - saying code has syntax error. The UDF is to add carriage return for all of the code:

String res = "";

int flag = 0;

for(int i = 0;i<xmlString.length();i++)

{

if(xmlString.charAt =='<')

{

  flag = 1;

  res = res + xmlString.charAt  ;

}

else

{

  if(xmlString.charAt =='>')

  {

   flag = 0;

   res = res +xmlString.charAt + "\n";

  }

  else

  {

   res = res + xmlString.charAt ;

  }

}

}

return res;

************

has syntax errors:  
Former Member
0 Kudos

Hi Ramkumar

Just to make sure:

You need to map ONLY source root node to UDF to target root node:

<SourceRootNode > --> UDF --> <TargetRootNode>

Thank you,

Ritu

Former Member
0 Kudos

Hi

This is the correct code

String res = "";

int flag = 0;

for(int i = 0;i<xmlString.length();i++)

{

if(xmlString.charAt(i) =='<')

{

  flag = 1;

  res = res + xmlString.charAt(i) ;

}

else

{

  if(xmlString.charAt(i) =='>')

  {

   flag = 0;

   res = res +xmlString.charAt(i) + "\n";

  }

  else

  {

   res = res + xmlString.charAt(i) ;

  }

}

}

return res;

Thanks,

Indrajit

Former Member
0 Kudos

Thanks!! Indrajit - that resolved syntax error!!

Also, wanted to clarify to have carriage return do I have to have the UDF only mapped to the root node:

<SourceRootNode>  ---->UDF---->  <TargetRootNode>

Thank you so much for your help!!

Ritu