cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Special Chars in XML input

Former Member
0 Likes
3,279

Hi Guru's,

I am doing a FIle(XML) to Proxy scenario. Unfortunately the file sent by 3rd party system contains some special chars like "&".

Here is what I get into XI. <Program>FY08 Pilsner - National Media & Fees</Program>. When I manually change it to <Program><![CDATA[FY08 Pilsner - National Media & Fees]]></Program> it works.

After reading some of the forum threads I came to know that we can do this using JAVA mapping before doing the actual graphical mapping. I need to what exactly I should be doing in JAVA mapping and I need some sample JAVA mapping code if somebody has already implemented this solution.

Thanks.

Srini Vaidyam

View Entire Topic
Former Member
0 Likes

Hi Srinivas,

Payload is not valid XML. What is your File sender system character set use it in the File Adapter for conversion.

& should be escaped (&amp) else you will face conversion errors.

Regards,

Former Member
0 Likes

Hi SriHari,

I cannot ask them to send the XML file in a different/correct format. All I can do is take it in and somehow change it using JAVA Mapping.

Can anbody help me in sharing some similar JAVA Code if you have faced similar problem.

Thanks.

Srini

Former Member
0 Likes

The java mapping code will look something like:

public class FixEncoding implements StreamTransformation {

public void execute(InputStream in, OutputStream out)

throws StreamTransformationException {

String instr;

instr = parseIStreamToString(in);

instr = instr.replace( '&', '&amp;amp;' );

instr = instr.replace( '>', '&amp;gt;' );

instr = instr.replace( '<', '&amp;lt;' );

instr = instr.replace( '\"', '&amp;quot;' );

out.write(instr.getBytes());

}

public String parseIStreamToString(InputStream is) {

BufferedReader din = new BufferedReader(new InputStreamReader(is));

StringBuffer sb = new StringBuffer();

try {

String line = null;

while ((line = din.readLine()) != null) {

sb.append(line + "\n");

}

} catch (Exception ex) {

ex.getMessage();

} finally {

try {

is.close();

} catch (Exception ex) {

}

}

return sb.toString();

}

}

Note I have left out the includes and might need to handle some try/catches with error handling.

Regards,

Jason

Edited by: Jason Currey on Feb 5, 2008 1:28 AM

Edited by: Jason Currey on Feb 5, 2008 1:30 AM

Edited by: Jason Currey on Feb 5, 2008 1:31 AM

stefan_grube
Active Contributor
0 Likes

You should not use these lines of code:

instr = instr.replace( '>', '&gt;' );
instr = instr.replace( '<'&lt;' );
instr = instr.replace( '\"', '&quot;' );

This would corrupt the XML.

The code must be more sophisticated, as you have to check, if an & amp; is already in your XML, then you would not change the &.

Regards

Stefan