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

converting XML to ABAP

Former Member
0 Likes
1,222

Hi Experts

I have a problem while converting XML data into an abap internal table ......in fact its executing without any errors but i am not getting any data as output.. my code is as below....

can you suggest where do i go wrong...

XML DATA:

<?xml version="1.0"?>
<sldinfo supplier_name="ComputerSystem" supplier_version="1.0" model_version="1.3.21">
  <group name="system1" group_type="SPECIFIC">
    <class name="SAP_ComputerSystem">
      <instance>
        <property name="Status">
          <value>OK</value>
        </property>
        <property name="VirtualRAMInMB">
          <value>173013</value>
        </property>
      </instance>
    </class>
  </group>
</sldinfo>

ABAP MAIN PROG

TYPE-POOLS abap.
CONSTANTS gs_file TYPE string VALUE 'C:	empsldinfo.xml'.
* This is the structure for the data from the XML file
TYPES: BEGIN OF ts_group,
         supplier_name(20)    TYPE c,
         supplier_version(4)  TYPE n,
         model_version(20)    TYPE n,
         name(10)             TYPE C,
         group_type(10)       TYPE C,
         name2(10)            TYPE C,
         name3(10)            TYPE C,
         value(30)            TYPE C,
       END OF ts_group.
DATA: gt_itab       TYPE STANDARD TABLE OF char2048.
* Table and work ares for the data from the XML file
DATA: gt_group     TYPE STANDARD TABLE OF ts_group,
      gs_group     TYPE ts_group.

DATA: gt_result_xml TYPE abap_trans_resbind_tab,
      gs_result_xml TYPE abap_trans_resbind.

DATA: gs_rif_ex     TYPE REF TO cx_root,
      gs_var_text   TYPE string.

CALL METHOD cl_gui_frontend_services=>gui_upload
  EXPORTING
    filename                = gs_file
  CHANGING
    data_tab                = gt_itab
****************************************************************
GET REFERENCE OF gt_group INTO gs_result_xml-value.
gs_result_xml-name = 'IGROUP'.
APPEND gs_result_xml TO gt_result_xml.

* Perform the XSLT stylesheet
TRY.
    CALL TRANSFORMATION z_xml_to_abap1
    SOURCE XML gt_itab
    RESULT (gt_result_xml).
CATCH cx_root INTO gs_rif_ex.
    gs_var_text = gs_rif_ex->get_text( ).
    MESSAGE gs_var_text TYPE 'E'.
ENDTRY.

* Now let´s see what we got from the file
LOOP AT gt_group INTO gs_group.
  WRITE: / 'supplier_name:   ' , gs_group-supplier_name.
  WRITE: / 'supplier_version :', gs_group-supplier_version.
  WRITE: / 'model_version :',    gs_group-model_version.
  WRITE : / 'name :',            gs_group-name  .
  WRITE: / 'group_type :',       gs_group-group_type.
  WRITE: / 'name2 : ',           gs_group-name2.
  WRITE: / ' name3 :',           gs_group-name3.
  WRITE: / '  value :' ,         gs_group-value.
ENDLOOP. "gt_group.  

TRANSFORMATION

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output encoding="iso-8859-1" indent="yes" method="xml" version="1.0"></xsl:output>
  <xsl:strip-space elements="*"></xsl:strip-space>

  <xsl:template match="/">
    <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
        <IGROUP>
          <xsl:apply-templates select="//GROUP"></xsl:apply-templates>
        </IGROUP>
      </asx:values>
    </asx:abap>
  </xsl:template>

  <xsl:template match="GROUP">
    <item>
      <SUPPLIER_NAME>
        <xsl:value-of select="supplier_name"></xsl:value-of>
      </SUPPLIER_NAME>
      <SUPPLIER_VERSION>
        <xsl:value-of select="supplier_version"></xsl:value-of>
      </SUPPLIER_VERSION>
      <MODEL_VERSION>
        <xsl:value-of select="model_version"></xsl:value-of>
      </MODEL_VERSION>
      <NAME>
        <xsl:value-of select="name"></xsl:value-of>
      </NAME>
      <GROUP_TYPE>
        <xsl:value-of select="group_type"></xsl:value-of>
      </GROUP_TYPE>
      <NAME2>
        <xsl:value-of select="name2"></xsl:value-of>
      </NAME2>
      <NAME3>
        <xsl:value-of select="name3"></xsl:value-of>
      </NAME3>
      <VALUE>
        <xsl:value-of select="value"></xsl:value-of>
      </VALUE>
    </item>
  </xsl:template>
</xsl:transform>

Regards

Albert

Hi Experts

I have a problem while converting XML data into an abap internal table ......in fact its executing without any errors but i am not getting any data as output.. my code is as below....

can you suggest where do i go wrong...

XML DATA:

<?xml version="1.0"?>
<sldinfo supplier_name="ComputerSystem" supplier_version="1.0" model_version="1.3.21">
  <group name="system1" group_type="SPECIFIC">
    <class name="SAP_ComputerSystem">
      <instance>
        <property name="Status">
          <value>OK</value>
        </property>
        <property name="VirtualRAMInMB">
          <value>173013</value>
        </property>
      </instance>
    </class>
  </group>
</sldinfo>

ABAP MAIN PROG

TYPE-POOLS abap.
CONSTANTS gs_file TYPE string VALUE 'C:	empsldinfo.xml'.
* This is the structure for the data from the XML file
TYPES: BEGIN OF ts_group,
         supplier_name(20)    TYPE c,
         supplier_version(4)  TYPE n,
         model_version(20)    TYPE n,
         name(10)             TYPE C,
         group_type(10)       TYPE C,
         name2(10)            TYPE C,
         name3(10)            TYPE C,
         value(30)            TYPE C,
       END OF ts_group.
DATA: gt_itab       TYPE STANDARD TABLE OF char2048.
* Table and work ares for the data from the XML file
DATA: gt_group     TYPE STANDARD TABLE OF ts_group,
      gs_group     TYPE ts_group.

DATA: gt_result_xml TYPE abap_trans_resbind_tab,
      gs_result_xml TYPE abap_trans_resbind.

DATA: gs_rif_ex     TYPE REF TO cx_root,
      gs_var_text   TYPE string.

CALL METHOD cl_gui_frontend_services=>gui_upload
  EXPORTING
    filename                = gs_file
  CHANGING
    data_tab                = gt_itab
****************************************************************
GET REFERENCE OF gt_group INTO gs_result_xml-value.
gs_result_xml-name = 'IGROUP'.
APPEND gs_result_xml TO gt_result_xml.

* Perform the XSLT stylesheet
TRY.
    CALL TRANSFORMATION z_xml_to_abap1
    SOURCE XML gt_itab
    RESULT (gt_result_xml).
CATCH cx_root INTO gs_rif_ex.
    gs_var_text = gs_rif_ex->get_text( ).
    MESSAGE gs_var_text TYPE 'E'.
ENDTRY.

* Now let´s see what we got from the file
LOOP AT gt_group INTO gs_group.
  WRITE: / 'supplier_name:   ' , gs_group-supplier_name.
  WRITE: / 'supplier_version :', gs_group-supplier_version.
  WRITE: / 'model_version :',    gs_group-model_version.
  WRITE : / 'name :',            gs_group-name  .
  WRITE: / 'group_type :',       gs_group-group_type.
  WRITE: / 'name2 : ',           gs_group-name2.
  WRITE: / ' name3 :',           gs_group-name3.
  WRITE: / '  value :' ,         gs_group-value.
ENDLOOP. "gt_group.  

TRANSFORMATION

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output encoding="iso-8859-1" indent="yes" method="xml" version="1.0"></xsl:output>
  <xsl:strip-space elements="*"></xsl:strip-space>

  <xsl:template match="/">
    <asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
      <asx:values>
        <IGROUP>
          <xsl:apply-templates select="//GROUP"></xsl:apply-templates>
        </IGROUP>
      </asx:values>
    </asx:abap>
  </xsl:template>

  <xsl:template match="GROUP">
    <item>
      <SUPPLIER_NAME>
        <xsl:value-of select="supplier_name"></xsl:value-of>
      </SUPPLIER_NAME>
      <SUPPLIER_VERSION>
        <xsl:value-of select="supplier_version"></xsl:value-of>
      </SUPPLIER_VERSION>
      <MODEL_VERSION>
        <xsl:value-of select="model_version"></xsl:value-of>
      </MODEL_VERSION>
      <NAME>
        <xsl:value-of select="name"></xsl:value-of>
      </NAME>
      <GROUP_TYPE>
        <xsl:value-of select="group_type"></xsl:value-of>
      </GROUP_TYPE>
      <NAME2>
        <xsl:value-of select="name2"></xsl:value-of>
      </NAME2>
      <NAME3>
        <xsl:value-of select="name3"></xsl:value-of>
      </NAME3>
      <VALUE>
        <xsl:value-of select="value"></xsl:value-of>
      </VALUE>
    </item>
  </xsl:template>
</xsl:transform>

Regards

Albert

8 REPLIES 8
Read only

Former Member
0 Likes
1,096

Hi,

Please check the below lin, it might be helpful.

<SAPTechnical Link removed>

Edited by: Suhas Saha on Sep 28, 2011 1:55 PM

Read only

Lukas_Weigelt
Active Contributor
0 Likes
1,096

Well it can't throw any error since you catch errors into root error class

Have you traced, whether it actually "crashes" within the TRY-CATCH phrase?

Also, usually XML Files should be called in a transformation in type XSTRING, not some data table

Are you required to upload the XML per hand with GUI_UPLOAD or is this just for testing?

Cheers, Lukas

Read only

0 Likes
1,096

hi Lukas

thank you for your kind reply

"crashes" within the TRY-CATCH phrase?

Yes your expectation was correct..

Are you required to upload the XML per hand with GUI_UPLOAD

it is to Get the XML file from my client....

what would you suggest to rectify my question...........

Regards

Albert

Read only

0 Likes
1,096

I suggest you take a look why it crashes. If you can't determine while debugging, remove the try-catch phrase and let it dump so you can look in st22.

Can you explain what your application is supposed to do eventually in a use case? I could suggest other approaches rather than the one you took, but I wouldn't know if they would meet your requirements.

From what you posted so far, I guess some end user has got a transaction and is uploading XML Files manually so its data is stored somewhere in SAP or processed otherwise.... ? .. Please clarify what the business function, the actual cause of this application is, then I can help you better

Cheers, Lukas

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,096

"crashes" within the TRY-CATCH phrase?

Yes your expectation was correct..

What does the exception object text(method get_text ) say? IMO you need not remove the TRY-CATCH block, rather check the corresponding exception obect generated.

As a suggestion try to use specific exception classes. In your code snippet you've used the generic "CX_ROOT", you must use CX_XSLT* exception classes.

BR,

Suhas

Read only

0 Likes
1,096

Just to rule out any misunderstandings, I only meant to remove the TRY-CATCH phrase for a blink of an eye so you have a short dump and don't have to trace all the time. Of course you should keep this in your coding bottom line ))

Read only

0 Likes
1,096

HI Lukas

Thats fine.........

I am almost there ..........

let me admit myself... i am new to XML

if possible could you copy and paste the above code(to find out the error) and execute on the fly.....

hope it won't take much of your time....

Regards

ALBERT

Edited by: albert_jack123 on Sep 28, 2011 3:02 PM

Read only

0 Likes
1,096

Hay, could you solve your problem and if yes could you please share the solution?

Thanks, Vanessa