10-03-2005 7:40 AM
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
10-03-2005 7:49 AM
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
10-03-2005 8:08 AM
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
10-03-2005 8:11 AM
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
10-03-2005 9:49 AM
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
10-03-2005 1:22 PM
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
10-03-2005 10:29 AM
Take a look at the cl_xml_document this has a method called GET_DATA which fills a datastructure out of your XML file.
10-03-2005 10:51 AM
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
10-03-2005 10:58 AM
'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