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

getting XML via ABAP

Former Member
0 Likes
2,583

Hi experts;

i want to download an xml file (central bank currency info XML) and read the currency info from xml file with abap.

can you help me about this. documentation , example code...

<b>i want to download xml from web first then upload this xml to an internal table.</b>

regards

Mustafa

Message was edited by:

Mustafa SADIK

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,738

Hello Mustafa,

the IXML library delievers all methods to retrieve and parse xml documents.

For your needs replace "uri_to_xml" with uri to your xml document.

With "find_from_path" you can get xml elements by simple xpath expression.

Hope it helps.

Regards

Sebastian


DATA: lr_ixml                TYPE REF TO if_ixml,
      lr_ixml_doc            TYPE REF TO if_ixml_document,
      lr_ixml_stream_factory TYPE REF TO if_ixml_stream_factory,
      lr_ixml_istream        TYPE REF TO if_ixml_istream,
      lr_ixml_parser         TYPE REF TO if_ixml_parser,
      lr_ixml_element        type ref to if_ixml_element,
      lp_value               type string.

lr_ixml                = cl_ixml=>create( ).
lr_ixml_doc            = lr_ixml->create_document( ).
lr_ixml_stream_factory = lr_ixml->create_stream_factory( ).

lr_ixml_istream = lr_ixml_stream_factory->create_istream_uri( uri_to_xml ).

lr_ixml_parser  = lr_ixml->create_parser( document       = lr_ixml_doc
                                          istream        = lr_ixml_istream
                                          stream_factory = lr_ixml_stream_factory ).

lr_ixml_parser->parse( ).

lr_ixml_element = lr_ixml_doc->find_from_path( path = '/root/some_node/currency' ).

lp_value = lr_ixml_element->get_value( ).

* do something *
*
* data: ln_amount type i.
*
* ln_amount = lp_value.

Hi experts;

i want to download an xml file (central bank currency info XML) and read the currency info from xml file with abap.

can you help me about this. documentation , example code...

<b>i want to download xml from web first then upload this xml to an internal table.</b>

regards

Mustafa

Message was edited by:

Mustafa SADIK

7 REPLIES 7
Read only

Former Member
0 Likes
1,738

hi,

You can use the FM TEXT_CONVERT_XML_TO_SAP

Regards,

Richa

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
1,738

Hi,

Here is the sample code from a website.

  • Databases

TABLES:

makt, "Mat description

marc, "Material / plant

t001w, "plant name

bhdgd. "Batch heading

  • Internal tables

DATA:

BEGIN OF gt_marc OCCURS 0,

werks LIKE marc-werks,

matnr LIKE marc-matnr,

END OF gt_marc,

  • Table to be downloaded as xml. Each line stores start and end tags

  • and the value

BEGIN OF gt_xml OCCURS 0,

line(120),

END OF gt_xml,

g_maktx(120).

  • User-input

SELECT-OPTIONS:

s_werks FOR marc-werks,

s_matnr FOR marc-matnr.

**********************************************************************

START-OF-SELECTION.

  • Extract all required data

PERFORM main_processing.

**********************************************************************

END-OF-SELECTION.

SORT gt_marc BY werks matnr.

LOOP AT gt_marc.

AT FIRST. "First tag must be root

CLEAR gt_xml.

gt_xml-line = ''.

APPEND gt_xml.

CLEAR gt_xml.

  • display data

FORMAT COLOR 2 ON.

WRITE 😕 gt_marc-matnr, makt-maktx.

FORMAT COLOR 2 OFF.

ENDLOOP.

  • The last tag must be the root closing tag --*

gt_xml-line = '</LOCATIONS>'.

APPEND gt_xml.

CLEAR gt_xml.

CALL FUNCTION 'DOWNLOAD'

EXPORTING

filename = 'C:PLANT1.XML'

filetype = 'ASC'

TABLES

data_tab = gt_xml.

**********************************************************************

TOP-OF-PAGE.

MOVE sy-title TO bhdgd-line1.

MOVE sy-repid TO bhdgd-repid.

MOVE sy-uname TO bhdgd-uname.

MOVE sy-datum TO bhdgd-datum.

MOVE '0' TO bhdgd-inifl.

MOVE '132' TO bhdgd-lines.

FORMAT INTENSIFIED ON COLOR COL_HEADING.

PERFORM batch-heading(rsbtchh0). "report header

*----


*

  • Form READ_PLANT

*----


*

FORM read_plant.

  • Get plant name

CLEAR t001w.

SELECT SINGLE name1

INTO t001w-name1

FROM t001w

WHERE werks EQ gt_marc-werks.

ENDFORM. " READ_PLANT

*----


*

  • Form MAIN_PROCESSING

*----


*

FORM main_processing.

  • Material and plant basic data

SELECT werks matnr

INTO TABLE gt_marc

FROM marc

WHERE werks IN s_werks

AND matnr IN s_matnr.

ENDFORM. " MAIN_PROCESSING

*----


*

  • Form READ_DESCRIPTION

*----


*

FORM read_description.

  • Material name

CLEAR g_maktx.

SELECT SINGLE maktx

INTO g_maktx

FROM makt

WHERE matnr EQ gt_marc-matnr

AND spras EQ 'E'.

  • Replace special character

DO.

REPLACE '&' WITH '*ù%;' INTO g_maktx.

IF NOT sy-subrc IS INITIAL. EXIT.ENDIF.

ENDDO.

DO.

REPLACE '*ù%;' WITH '&amp;' INTO g_maktx.

IF NOT sy-subrc IS INITIAL. EXIT.ENDIF.

ENDDO.

DO.

REPLACE '/' WITH '&#47;' INTO g_maktx.

IF NOT sy-subrc IS INITIAL. EXIT.ENDIF.

ENDDO.

ENDFORM. " READ_DESCRIPTION

                                        • END OF PROGRAM Z_DOWN_XML ************************

Read only

Former Member
0 Likes
1,738

thanks i will look at this fm

and sorry i forget something i want to downnload xml from web according to given url

can you help me about this too.

regards

mustafa

<b>i think i made a mistake

i want to download xml from web first then upload this xml to an internal table.</b>

Message was edited by:

Mustafa SADIK

Read only

Former Member
0 Likes
1,739

Hello Mustafa,

the IXML library delievers all methods to retrieve and parse xml documents.

For your needs replace "uri_to_xml" with uri to your xml document.

With "find_from_path" you can get xml elements by simple xpath expression.

Hope it helps.

Regards

Sebastian


DATA: lr_ixml                TYPE REF TO if_ixml,
      lr_ixml_doc            TYPE REF TO if_ixml_document,
      lr_ixml_stream_factory TYPE REF TO if_ixml_stream_factory,
      lr_ixml_istream        TYPE REF TO if_ixml_istream,
      lr_ixml_parser         TYPE REF TO if_ixml_parser,
      lr_ixml_element        type ref to if_ixml_element,
      lp_value               type string.

lr_ixml                = cl_ixml=>create( ).
lr_ixml_doc            = lr_ixml->create_document( ).
lr_ixml_stream_factory = lr_ixml->create_stream_factory( ).

lr_ixml_istream = lr_ixml_stream_factory->create_istream_uri( uri_to_xml ).

lr_ixml_parser  = lr_ixml->create_parser( document       = lr_ixml_doc
                                          istream        = lr_ixml_istream
                                          stream_factory = lr_ixml_stream_factory ).

lr_ixml_parser->parse( ).

lr_ixml_element = lr_ixml_doc->find_from_path( path = '/root/some_node/currency' ).

lp_value = lr_ixml_element->get_value( ).

* do something *
*
* data: ln_amount type i.
*
* ln_amount = lp_value.

Read only

0 Likes
1,738

Sebastian:

The following code generates essentially a null pointer exception when I try to execute an XSLT transform (CALL TRANSFORM) on the <b>if_ixml_istream</b> named <i>istream</i>. The URL is valid as I've copied it into my browser and displayed the XML document from the server (I've obfuscated the actual URL for security reasons). Any suggestions?


TYPE-POOLS: abap.
TYPE-POOLS: ixml.
CLASS cl_ixml DEFINITION LOAD.

DATA g_ixml         TYPE REF TO if_ixml.
DATA streamfactory  TYPE REF TO if_ixml_stream_factory.
DATA istream        TYPE REF TO if_ixml_istream.
DATA xmlstr         TYPE string.
DATA ex             TYPE REF TO CX_SY_REF_IS_INITIAL.
DATA imsg           TYPE REF TO IF_MESSAGE.
DATA smsg           TYPE string.

START-OF-SELECTION.

  g_ixml = cl_ixml=>create( ).
  streamfactory = g_ixml->create_stream_factory( ).
  istream = streamfactory->create_istream_uri( 'http://www.someremotesite.com/documents/docindex.xml' ).

  TRY.
    CALL TRANSFORMATION zsimplexslt
      SOURCE XML istream
      RESULT XML xmlstr.
  CATCH CX_SY_REF_IS_INITIAL INTO ex.
    imsg = ex.
    WRITE / 'EXCEPTION THROWN:'.
    smsg = imsg->GET_LONGTEXT( ).
    smsg = imsg->GET_TEXT( ).
    WRITE / smsg.
  ENDTRY.

Read only

0 Likes
1,738

Is there only a simple xpath expression possible? Or can I also use following expression:

/ORDERS05/IDOC/E1EDKA1[PARVW='AG']/PARTN

If no, are there any other ABAP/4 statements to solve this or only a mini-transformation (also in this thread).

Kind regards

Helmut

> Hello Mustafa,

>

> the IXML library delievers all methods to retrieve

> and parse xml documents.

> For your needs replace "uri_to_xml" with uri to your

> xml document.

> With "find_from_path" you can get xml elements by

> simple xpath expression.

>

> Hope it helps.

>

> Regards

> Sebastian

>

>


> DATA: lr_ixml                TYPE REF TO if_ixml,
> lr_ixml_doc            TYPE REF TO
>  if_ixml_document,
> lr_ixml_stream_factory TYPE REF TO
>  if_ixml_stream_factory,
> lr_ixml_istream        TYPE REF TO
>  if_ixml_istream,
> lr_ixml_parser         TYPE REF TO
>  if_ixml_parser,
> lr_ixml_element        type ref to
>  if_ixml_element,
>      lp_value               type string.
> ixml                = cl_ixml=>create( ).
> lr_ixml_doc            = lr_ixml->create_document(
> ).
> lr_ixml_stream_factory =
> lr_ixml->create_stream_factory( ).
> 
> lr_ixml_istream =
> lr_ixml_stream_factory->create_istream_uri(
> uri_to_xml ).
> 
> lr_ixml_parser  = lr_ixml->create_parser( document
>       = lr_ixml_doc
> istream        =
> lr_ixml_istream
> 
> tream_factory = lr_ixml_stream_factory ).
> 
> lr_ixml_parser->parse( ).
> 
> lr_ixml_element = lr_ixml_doc->find_from_path( path =
> '/root/some_node/currency' ).
> 
> lp_value = lr_ixml_element->get_value( ).
> 
> * do something *
> *
> * data: ln_amount type i.
> *
> * ln_amount = lp_value.
> 
> 

Read only

Former Member
0 Likes
1,738

thanks Sebastian.

i reward your answer

thanks again