Convert Xml To String In Sap Free Download For Mac

  1. Mini Sap Free Download

Guide towards a simple conversion of an XML file to ABAP Internal table, using XML parsing

By Aastha Mehrotra, Larsen & Toubro Infotech

Applies to:

SAP ECC 6.0. And further.

Summary

The purpose of this document is to give an idea on Excel's XLS file format to XML file conversion using JAVA Mapping in SAP Xi 7.0. This document will be having the JAVA code along with screenshots of sample scenario created to test the JAVA mapping logic. I am presuming that developers have idea of. Sap file convert to xml file Free Download, sap file convert to xml file, convert binary files to xml files, want to convert idoc into xml in sap, convert pdf to xml sap and more. Download XML2CSV Spreadsheet Converter for free. Converts XML to CSV using a spreadsheet app. An MS Excel VBA macro converts XML to CSV. It demonstrates this capability by converting an XML-based Continuity of Care Document (CCD) into a slim Comma Separated Value (CSV) file.

This article elaborates the conversion process of an XML file residing in the SAP Application server, into an ABAP internal table.

An effortless approach, unlike the other intricate methods available for conversion, has been presented here. In this particular concept, initially, the XML file is converted into a string. Hence, we use the XML parsing technique to convert that string into an ‘x’ string and finally into an internal table.

A simple conversion idea, along with the supporting code blocks, has been demonstrated in this document.

Introduction

Various practices and procedures are available for XML-ABAP conversion. However, I discovered that, none of those takes us entirely across the process of converting an XML residing in the Application Server into an internal table in ABAP. Certain other methods, which might actually take us across, are difficult to comprehend and involve a number of complicated steps.

I used this method while working on an object which required me to

  • Fetch a request XML file from the Application server.

  • Convert it into an internal table in ABAP.

  • Extract data from ERP corresponding to the data in the internal table (from the XML file).

  • Generate a Response file consisting of both XML file data as well as data from ERP.

This document shall let you know the entire process involving the evolution of the XML file in the Application server to an Internal Table.

Following is an archetype of the XML file which needs to be converted into an internal table in ABAP.

This XML file carries data for three fields- HomePernr, UNAME and USERID. We need to convert the XML file into an internal table comprising of these three fields, and holding the three records from XML.

We would be considering the example of the following file through the course of this document. This would assist us convert any XML containing any number of fields and records into an internal table, in an uncomplicated manner.

Note:FileFeed happens to be the root node for the XML.

EmployeeRequests is the element node containing the separate Employee records within it.

Employeerequest is the element node for each record.

HomePERNR, UNAME and USERID are the value nodes containing the values for the records.

The entire mechanism would be elaborated by this document in several steps, supported with the appropriate code snippets.

The series of execution would be as follows:

  • Open the XML in order to read the data.

  • Transfer the contents into an internal table.

  • Concatenate the lines of the internal table into a string.

  • Convert the string into a hexadecimal string - The ‘X’ string.

  • Parse the ‘x’ string to convert the data into an XML table.

  • Read the XML table to transfer the data into the table with the required structure.

Note:The code snippets are actually parts of a single code, in a sequence. Hence, these can be conjoined together to achieve the entire code for XML_ABAP conversion.

We shall advance with the specification of the step wise conversion process. However, we must be aware of the variables to be declared, in advance. This would enhance our understanding of the steps and would provide clarity to the context.

Type declarations for the variables

Following are the declarations for all the variables to be used in the code snippets throughout the document. This would help us avoid all the confusion pertaining to the type of variables used.

*Declaring the file type

DATA:g_unixfilename TYPE zpathfile.'UNIX file path.

*Declaring the structure for the XML internal table

TYPES: BEGIN OF ty_xml,

raw(2000) TYPE c,

END OF ty_xml.

*Declaring the XML internal table

DATA:g_t_xml_tab TYPE TABLE OF ty_xml INITIAL SIZE 0.

*Declaring the work area for the XML internal table

DATA:wa_xml_tab TYPE ty_xml.

*Declaring the string to contain the data for the XML internal table

DATA:g_str TYPE string.

*Declaring the string to contain x string

DATA:g_xmldata TYPE xstring.

*Declaring the table to contain the parsed data

DATA:g_t_xml_info TYPE TABLE OF smum_xmltb INITIAL SIZE 0.

*Declaring the work area for the internal table containing the parsed data

DATA:g_s_xml_info LIKE LINE OF g_t_xml_info.

*Declaring the table to contain the returned messages from the parsing FM

DATA:g_t_return TYPE STANDARD TABLE OF bapiret2.

*Declaring the work area for the return table

DATA:wa_return LIKE LINE OF g_t_return.

*Declaring the structure for the table containing fields in the XML file

TYPES: BEGIN OF struc_people,

homepernr(8),

Uname(4) TYPE c,

Userid(32),

END OF struc_people.

*Declaring the internal table containing the fields in the XML file

DATA:g_t_employeerequest TYPE TABLE OF struc_people.

*Declaring the work area for the internal table containing the fields in the*XML file

DATA:g_s_employeerequest LIKE LINE OF g_t_employeerequest.

1.Open the XML to read data

The first breakthrough would be to open the XML file and read the data using the OPEN DATASET statement.

The addition IN TEXT MODE, to the OPEN DATASET opens the file as a text file. The addition ENCODING defines how the characters are represented in the text file. While writing the data into a text file, the content of a data object is converted to the representation entered after ENCODING, and transferred to the file.

* Open the XML file for reading data

OPEN DATASET g_unixfilename FOR INPUT IN TEXT MODE ENCODING DEFAULT.

Convert Xml To String In Sap Free Download For Mac

IF sy-subrc NE 0.

Sap free download for windows 10

MESSAGE ‘Error opening the XML file’ TYPE ‘E’.

ELSE.

2.Transfer the contents into an internal table of a specific type.

The next furtherance would be to move the contents of the file to an internal table. Following is the piece of code which would help us Read the data from the file into an internal table.

We use the READ DATASET statement to read the data from the file.

continued........

DO.

* Transfer the contents from the file to the work area of the internal table

READ DATASET g_unixfilename INTO wa_xml_tab.

IF sy-subrc EQ 0.

CONDENSE wa_xml_tab.

*Append the contents of the work area to the internal table

APPEND wa_xml_tab TO g_t_xml_tab.

ELSE.

EXIT.

ENDIF.

ENDDO.

ENDIF.

* Close the file after reading the data

CLOSE DATASET g_unixfilename.

3.Concatenate the lines of the internal table into a string.

Next, we move the lines of the internal table into a string. Hence, we get a string containing the data of the entire XML file.

continued........

*Transfer the contents from the internal table to a string

IF NOT g_t_xml_tab IS INITIAL.

CONCATENATE LINES OF g_t_xml_tab INTO g_str SEPARATED BY space.

ENDIF.

4.Converting the normal string into an ‘X’ string.

We need to convert the string thus formed into a ‘x’ string or a hexadecimal string using the function Module 'SCMS_STRING_TO_XSTRING'.The type xstring allows a hexadecimal display of byte chains instead of the presentation to the base of 64.

continued........

* The function module is used to convert string to xstring

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

EXPORTING

text= g_str

IMPORTING

buffer = g_xmldata

EXCEPTIONS

failed = 1

OTHERS = 2.

IF sy-subrc<> 0.

MESSAGE ‘Error in the XML file’ TYPE ‘E’.

ENDIF.

Some time ago, I was looking for some demonstration how to get data from SAP into an XML file and vice versa, but found nothing satisfactory on the web. So I had to start developing the 'try and error' method. By now, I believe, I've gathered enough knowledge to write that tutorial myself. Well, here it comes, and with it the complete ABAP and XSLT source code, and hopefully enough explanation that you'll be able to develop an understanding what is happening, and to use it for your own projects.

Part 1: XML to ABAP

To get started, we first of all need a structure within SAP that has to be mapped onto certain XML elements and vice versa. We want to keep things very simple, so let's say a data set shall be of a type person with the fields customer_id, first_name and last_name.

It's easy to put this into an ABAP type:

Free

It's also easy to think of an XML structure that can contain data of this type:

And now we're going to shift the data from the above XML file into an internal ABAP table of the type ts_person. The ABAP report to accomplish this task consists of two parts. The first part is a regular ABAP programme, the second part is an XSLT transformation. SAP supports this standard via CALL TRANSFORMATION. But more about that later.

Now copy the following code into your ABAP editor, and do read the comments to understand what's going on:

Now we can launch the SAP XSLT editor by double clicking on the transformation z_xml_to_abap. Depending on your SAP system, you'll have to choose the transformation type. XSLT is correct here. You'll see that SAP has already created a simple XSLT frame. Replace it with the following stylesheet:

XSLT is an internet standard developed by the W3 consortium. It's supported by recent versions of most internet applications and programming suites. For example, it's included in Internet Explorer, Firefox, .NET and Java. Whereas XSLT version 1.0 can't do much more than transforming XML files from one format into another, XSLT version 2.0 can be used to solve simple programming tasks. That newer version, however, isn't supported by SAP yet.

When transforming data from or to the ABAP format, a special XML dialect called ABAP-XML is used. The SAP kernel then converts this format into ABAP internal tables or the internal tables to ABAP-XML.

Now the first part is done. This programme will successfully extract the data from the XML file and import it in an internal table. Try it by saving the XML example file in the location C:tempcustomers.xml and run the report. You can also try to alter the XSLT and the XML file, and see how it affects the result.

Part 2: ABAP to XML

The other direction from ABAP to XML works exactly the other way around. Again, we're using the type definition and the XML structure from part 1. We're appending a couple of test values to the internal table, and apply another XSLT transformation to this data. Copy the following source code into your ABAP editor, and don't forget to read the comments:

Of course, the second report also needs an XSLT stylesheet. Create it by double clicking the transformation. Then insert the following source code into the XSLT editor:

Now run your report. It'll create a valid XML file for you from the two lines of the internal table. Here you can also try to change the XSLT stylesheet, and see how your alterations affect the resulting XML file.

Final remarks

There are two more things I'd like to finally let you know. This tutorial has just shown you only the tip of the iceberg. We've used only a flat structure on the ABAP side, and the input/output XML format is also kind of flat. The XML formats in broad use are way more complex, just think of SVG, XHTML or the Microsoft Office XML files. XSLT can easily manage these complex formats as well, even from within SAP, and extract certain data from these files, or generate complex XML files containing data from the SAP tables.

The second issue I want to raise is of the code page. Of course, the code page of the XML files has to be the same one as the code page of the SAP system. Also, when downloading the generated XML file to your computer, you'll have to select the correct corresponding code page.

You can work around this restriction if you don't use string and char data types, but xstring and raw instead. In this case, you'll have to upload and download your files in binary type. It's even possible to create UTF-8 encoded XML files in a non-unicode SAP system! The examples in this tutorial are all made to work with SAP Netweaver Trial 7.02 SP6. It's possible that you'll have to adapt the choice of code pages if you want to run them on another system.

What I can offer for your company is more than three years of experience of XSLT integration in SAP, especially when it comes to generating complex XML files, or extracting data from XML files and storing it within SAP tables. I can also offer professional training for your IT department. Please get in touch and ask for a quote. Based in Cambodia, we can offer you high quality service at very good rates.

Get deeper into XSLT

Although a bit old, but not the least outdated, the best XSLT tutorial and reference book I've come across is 'Inside XSLT' by Steven Holzner. If you want to parse or build deeply nested XML documents, this book will help you understand XSLT in every aspect.

Mini Sap Free Download

Every single XSLT element and XPath function is explained in detail with more than 400 code examples, for example, how to sort elements, how to use loops and if conditions, and how to convert data to other formats like HTML.