Introduction: I recently came across an unusual scenario where we were sending an XML file to a customer whose system was unable to read the entire file.
Reason: The system could not read more than 240 characters because all the data was on a single line and there was a character limit for each row (see below image).
To build the XML file, we used the call transformation method, supplying the source as an internal table.
CALL TRANSFORMATION z_generate_xml
SOURCE source_doc = lt_xml_data
RESULT XML DATA(lv_xml_as_xstring).
Replication:
In this instance, when we download the same XML data to our desktop and double click to open it, it usually prompts us with a location for opening, and if we choose to open it in any browser, it automatically formats it in a neat XML aligned manner. First impressions suggest that there is no issue. However, in order to imitate how it would be received in any other system, we opened it with Notepad++ (or any similar system). There, we observed the generation of XML, and to our surprise, all of the data appeared in a single line as per below image.
Solution: To resolve this issue, we can convert XSTRING data into STRING format by passing it into method cl_bcs_convert=>xstring_to_string( ).
cl_bcs_convert=>xstring_to_string( EXPORTING
iv_xstr = im_file_data
iv_cp = '1100'
RECEIVING
rv_string = DATA(lv_string) ).
Next, build an XSLT transformation (z_xml_pretty_print) with an output method of 'xml' and indent 'yes'.
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:transform>
Next, invoke this new transformation using string data as both the source and target.
CALL TRANSFORMATION z_xml_pretty_print
SOURCE XML lv_string
RESULT XML lv_string.
Last but not least, add two lines of code to ensure that XML works properly. First split the data into an internal table and replace first line with xml version and encoding. The same version and encoding will be present on the first line, but the first character will be a newline, and in the file it will display as #, indicating a corrupt file.
SPLIT lv_string AT |\n| INTO TABLE DATA(li_record).
li_record[ 1 ] = '<?xml version="1.0" encoding="UTF-8"?>'.
We can now see everything in nice XML (pretty print) format.
The functionality ends at this point. I'm grateful that you took the time to read this. I hope that helps you. Feel free to post any recommendations you may have.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
3 | |
2 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 |