Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Reading XML Data from ABAP Program?

Former Member
0 Likes
2,686

Hi,

How do I read XML Data from an ABAP Program? For example if I have the below basic XML Code-

<xml>

<Name> Thiru </Name>

<Age> 24 </Age>

<City> chennai </Chennai>

</xml>

How do i read the data within the Name,Age, and City tags into variables in the ABAP Program?

Regards,

Thiru

8 REPLIES 8
Read only

Former Member
0 Likes
1,267

Hi,

Check this link,

<u>http://www.sap-img.com/abap/xml-file-to-word-document-through-sap.htm</u>

Hope it helps u.

Thanks&Regards,

Ruthra.R

Read only

athavanraja
Active Contributor
0 Likes
1,267

You need to write a XSLT program to convert this XML into a variable/itab.

and then use the XSLT program along with

CALL TRANSFORMATION to do the actual conversion.

CALL TRANSFORMATION (`<z_myxslt_program`)

SOURCE XML <source xml>

RESULT itab = itab[].

search ABAP forum with key word CALL TRANSFORMATION and you will see quiet a few examples with code.

Regards

Raja

Read only

Former Member
0 Likes
1,267

hi, there is two way for you choose, DOM or XSLT.

for DOM , you can read this link for reference:

http://help.sap.com/saphelp_nw04/helpdata/en/86/8280ba12d511d5991b00508b6b8b11/frameset.htm

for XSLT, it only supported on 4.7 or above, here is a topic on how to read XML into ABAP with XSLT:

hope it will be helpful

thanks

Read only

Former Member
0 Likes
1,267

Hi durai,

I am able to read the code into an internal table but how do i specify the tag and get the data within the specific tag?

I suppose call transformation is used for seriazliation right? How do i deserialize?

Regards,

Thiru

Read only

0 Likes
1,267

hi,

define one internal table as like the node sequence in XML, then the data will automatically poulate into internal table by call transformation method

cheers,

sasi

Read only

Former Member
0 Likes
1,267

Take a look at the cl_xml_document this has a method called GET_DATA which fills a datastructure out of your XML file.

Read only

Former Member
0 Likes
1,267

if you decide to do in XSLT, I have a sample list here:


XML file like this:
<?xml version="1.0" encoding="UTF-16"?>
<F>
<P1>
<t_1>value1</t_1>
<t_2>testvalue</t_2>
</P1>
<P2>
</P2>
</F>

XSLT file like this:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sapxsl="http://www.sap.com/sapxsl" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="F">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<<b>DOCUMENT</b>>
<xsl:apply-templates/>
</<b>DOCUMENT</b>>
</asx:values>
</asx:abap>
</xsl:template>
<xsl:template match="P1">
<ENTRY>
<<b>T_1</b>><xsl:value-of select="t_1"/></T_1>
<<b>T_2</b>><xsl:value-of select="t_2"/></T_2>
</ENTRY>
</xsl:template>
</xsl:transform>

ABAP program like this:
DATA: BEGIN OF wa_upload,
text(255) TYPE c,
END OF wa_upload,
itab_upload LIKE TABLE OF wa_upload,
BEGIN OF wa_document,
t_1 TYPE string,
t_2 TYPE string,
END OF wa_document,
itab_document LIKE TABLE OF wa_document.


CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = 'XXXXX'
filetype = 'ASC'
TABLES
data_tab = itab_upload.

CALL TRANSFORMATION zrappel_xml_test
SOURCE XML itab_upload
RESULT <b>document</b> = itab_document.

You should pay attention to the bold words.

hope it will be helpful

thanks

Read only

Former Member
0 Likes
1,267

'but how do i specify the tag and get the data within the specific tag'

You should do it in the XSLT file.

XSLT has <if ...> and you can define variable in XSLT.

you can search some material of XSLT, if you only need to specify a tag, and get the value of this tag, do like this:

<xsl:value-of select="XXXX"/>

XXX is a path of XSLT, hope you are familiar with XPATH.

thanks