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

Parse XML to ABAP

Former Member
0 Likes
2,237

HI Expert,

i have a big Problem to parse XMP to ABAP into an internal table with name and value,

and i will be very happy if i got an answer,

here a coding of xml ( example)

<?xml version='1.0' encoding='utf-8'?><!-- MyXmlFile.xml -->

<GUI-Description>

<Button size="normal" x="70" y="80">

<Title>Titel1</Title>

<Action>Action1</Action>

<Comment/>

</Button>

<Button size="small">

<Title>Second titel</Title>

<Action>Second action</Action>

</Button>

<Button size="big">

<Action>Action 3</Action>

<Title>Title 3</Title>

<Comment>here is the problm <test>HTML</test>-<abc>Text</abc>.</Comment>

</Button>

</GUI-Description>

withing parsing i i got tow times' TEST' as a name and the value is ' HTML' the first time, and the second time is '-'

and the same problem with 'abc' too. and 'coment' as a name and 'this is the problem' as a value.

<Comment>here is the problm <test>HTML</test>-<abc>Text</abc>.</Comment>

i want only th childs node and not the parents and here is my coding:

iterator = node->create_iterator( ).

  • get current node

node = iterator->get_next( ).

  • loop over all nodes

while not node is initial.

      • Check if the node is an element or a text node.

case node->get_type( ).

when if_ixml_node=>co_node_element.

  • element node

name = node->get_name( ).

ls_string-name = name.

when if_ixml_node=>co_node_text.

  • text node

value = node->get_value( ).

ls_string-value = value.

  • fill the internal table

append ls_string to gt_result.

endcase.

  • ENDIF.

  • advance to the next node

node = iterator->get_next( ).

endwhile.

thanx in advance Expert

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,039

perhaps use transformation

works like a charm to transfer xml to internal tables

CALL TRANSFORMATION ('ID')
    SOURCE XML it_xml_import
    RESULT order_header_in = st_sdhd1 order_conditions_in = ta_cond  order_partners = ta_parnr  order_text = ta_sdtext order_items_in = ta_sditm
      .

16 REPLIES 16
Read only

Former Member
0 Likes
2,040

perhaps use transformation

works like a charm to transfer xml to internal tables

CALL TRANSFORMATION ('ID')
    SOURCE XML it_xml_import
    RESULT order_header_in = st_sdhd1 order_conditions_in = ta_cond  order_partners = ta_parnr  order_text = ta_sdtext order_items_in = ta_sditm
      .

Read only

0 Likes
2,039

HI A. de Smidt ,

i have already trayed Call transformation, it didnt work, beacause i eant that coding not only for that example , but for all kind of xml-file with child nodes, but please explame me again the Parameters that you have just use for Result: i will try it again and see if it will works

thanx a lot

Read only

0 Likes
2,039

here is the xml file that I process with child nodes as well.

<?xml version="1.0" encoding="utf-8"?>
<asx:abap xmlns:asx="http://www.sap.com/abapxml">
<asx:values>

      <ORDER_HEADER_IN>
        <DOC_TYPE>TA</DOC_TYPE>
        <SALES_ORG>6300</SALES_ORG>
        <DISTR_CHAN>00</DISTR_CHAN>
        <DIVISION>00</DIVISION>
        <REQ_DATE_H>2008-08-19</REQ_DATE_H>
        <PURCH_NO_C>12345</PURCH_NO_C>
        <PURCH_DATE>2008-08-11</PURCH_DATE>
        <NAME>arthur</NAME>
      </ORDER_HEADER_IN>
 
      <ORDER_CONDITIONS_IN>
        <item>
          <ITM_NUMBER>000010</ITM_NUMBER>
          <COND_TYPE>PR00</COND_TYPE>
          <COND_VALUE>250.00</COND_VALUE>
        </item>
      </ORDER_CONDITIONS_IN>
      <ORDER_ITEMS_IN>
        <item>
          <ITM_NUMBER>000010</ITM_NUMBER>
          <MATERIAL>87100-A9</MATERIAL>
          <TARGET_QTY>1</TARGET_QTY>
          <ORDERID>000006405394</ORDERID>
          <SHORT_TEXT>bla</SHORT_TEXT>
        </item>
      </ORDER_ITEMS_IN>


      <ORDER_PARTNERS>
        <item>
          <PARTN_ROLE>AG</PARTN_ROLE>
          <PARTN_NUMB>1199</PARTN_NUMB>
        </item>

      </ORDER_PARTNERS>
  
      <ORDER_TEXT>
        <item><TEXT_LINE>regel1</TEXT_LINE></item>
        <item><TEXT_LINE>regel2</TEXT_LINE></item>
        <item><TEXT_LINE>voorbereiding:</TEXT_LINE></item>
      </ORDER_TEXT>

</asx:values>
</asx:abap>

after the gui upload that returns ta_xml_import I transform the xml stream with

DATA: it_xml_import TYPE string.

DATA: ta_xml_import TYPE STANDARD TABLE OF string.
DATA: wa_xml_import TYPE string.

    LOOP AT ta_xml_import INTO wa_xml_import.
      CONCATENATE it_xml_import wa_xml_import INTO it_xml_import.

    ENDLOOP.

the data from the xml is returned in bapi structures that I use directly for BAPI_SALESORDER_CREATEFROMDAT2

DATA:
  st_sdhd1   LIKE bapisdhd1,

  ta_sdtext TYPE TABLE OF bapisdtext,
  wa_sdtext TYPE bapisdtext,

  ta_schdl TYPE TABLE OF bapischdl,
  wa_schdl TYPE bapischdl,

kind regards

arthur

Read only

0 Likes
2,039

HI A. de Smidt ,

Thanx a lot for your hepl, but with your xml-file , my coding is working perfect, without any problem,

but the problem is as i said, that there is ELEMENT as a value 'TEXT'

<Button size="big">

<Action>Action3</Action>

<Title>title3</Title>

-


> <Comment>Here is the problem <test>HTML</test>-<abc>Text</abc>.</Comment> <----


</Button>

i dont want <Comment> in my internal table with its value 'HERE IS THE PROBLEM'' ,

i want only the child nods ( <test>HTML</test> and <abc>Text</abc> )

the problem that i have is that, there is child nods withing the 'TEXT'

Thank you very much for your help

Read only

0 Likes
2,039

Hi,

this is your result (gt_result):


Comment : here is the problm   
test : HTML                    
test : -                       
abc : Text                     
abc : .                        

in your loop you'll get the following nodes from the iterator:

01. node_element: Comment

02. node_text: here is the problm => Comment/here is the problm to result_table

03. node_element: test

04. node_text: 'HTML' => test/HTML to result_table

05. node_text: '-' => test/- to result_table

06. node_element: abc

07. node_text: 'Text' => abc/Text to result_table

08. node_text: '.' => abc/. to result_table

that's why you get the result at least.

regards, Karsten

Read only

0 Likes
2,039

Hi Karsten Korte ,

thanx a lot, you got really my problem, but please i still need some explanation , how can solve that problem in my coding,

thanx

Read only

0 Likes
2,039

Hi jimkim,

ok, just to be sure. You want


test : 'HTML'                 
abc : 'Text'

without the 'Comment'-element

and not


Comment : 'here is the problm <test>HTML</test>-<abc>Text</abc>.'

or


Comment : 'here is the problm'
test : 'HTML'                 
abc : 'Text'

?

Read only

0 Likes
2,039

HI Karsten Korte ,

as you said, only

test : HTML

abc: TEST

without Coment.

thank you very much for your help

Read only

0 Likes
2,039

Hi Karsten Korte ,

as you've said

test : 'HTML'

abc : 'Text'

without Coment and its node_text

only

test : 'HTML'

abc : 'Text'

Read only

0 Likes
2,039

you can skip the parent and iterate over the children:


*  iterator = node->create_iterator( ).
  node_list = node->get_children( ).
  iterator = node_list->create_iterator( ).

in this case you must not use the text-nodes:


*** Check if the node is an element or a text node.
            CASE node->get_type( ).
              WHEN if_ixml_node=>co_node_element.
* element node
                ls_string-name = node->get_name( ).
                ls_string-value = node->get_value( ).
                APPEND ls_string TO gt_result.
              WHEN if_ixml_node=>co_node_text.
** text node
*                ls_string-value = node->get_value( ).
** fill the internal table
*                APPEND ls_string TO gt_result.
            ENDCASE.

but this only works for node = 'Comment'-node. When you start at root node it would not iterate over the childs of the children.

Read only

0 Likes
2,039

Hi Karsten Korte ,

thanx for the Tipps, but i still have the same problem with parents node , is there any methods that i can get rid of them,

i have already tryed get_children and then get_length, but i get all nodes inside , wahtever text or a child, and get_depth not working good and get num_children too

thanx a lot

Read only

0 Likes
2,039

you get text-nodes since they are childs too, so you have to use the filter for element-nodes.

Could you please tell us what the contents of gt_result should be at the end, which name-value-pairs should be included there from your example?

regrads, Karsten

Read only

0 Likes
2,039

Hi Karsten Korte ,

this is an example of xml-file

<?xml version='1.0' encoding='utf-8'?><!-- MyXmlFile.xml -->

<GUI-Description>

<Button size="normal" x="70" y="80">

<Title>Titel1</Title>

<Action>Action1</Action>

<Comment/>

</Button>

<Button size="small">

<Title>Second titel</Title>

<Action>Second action</Action>

</Button>

<Button size="big">

<Action>Action 3</Action>

<Title>Title 3</Title>

<Comment>here is the problm <test>HTML</test>-<abc>Text</abc>.</Comment>

</Button>

</GUI-Description>

my result table should be like that:

-


NAME : VALUE

-


Title Titel1

Action Action1

Title Second tite

Action Second Action

Action Action 3

Title Title3

test HTML

abc text

-


kind regards

Read only

0 Likes
2,039

thank you very much, problem is solved 😆

Read only

0 Likes
2,039

Hey Jimkim

Could you please post the solution ?

Thanks

Sameer

Read only

0 Likes
2,039

Hi Samir,

you have to check number of children with the method num_children

if num_children = 1 then everything is ok.

herzliche Grüße

Jimy

Edited by: jimkim on Oct 23, 2009 1:16 PM

Edited by: jimkim on Oct 23, 2009 1:17 PM