Application Development 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: 

XML Parser

former_member216168
Active Participant
0 Kudos

Hi there,

I'm facing a problem while I try to parse a XML file.

This is my code:

DATA:  o_xml_document TYPE REF TO cl_xml_document.

DATA: v_tag_value TYPE string.

CREATE OBJECT o_xml_document TYPE cl_xml_document.

o_xml_document->parse_xstring( v_xml_xstring ). " <<<<--- it's ok, I already have this

v_tag_value = o_xml_document->find_simple_element( name = 'infCarga'). <<--- this is the tag name that I need


WRITE: / v_tag_value. "


Result:


33398.98FRASCO VIDROPALLET03VOLUMES7819.936003VOLUMES16.0000


I would like to get tag names and values... (vCarga = 333398.98, proPred=......)


Does anyone could suggest me something?


1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor
0 Kudos

You can either create the simple transformation to convert the XML into your data structure. Or else, you would need to loop through the kids of the specific node.

DATAo_xml_document TYPE REF TO cl_xml_document.

DATA: v_tag_value TYPE string.
CREATE OBJECT o_xml_document TYPE cl_xml_document.

o_xml_document
->import_from_file(
filename
= 'C:\temp\test_file.xml').
DATA: lo_node TYPE REF TO if_ixml_node.

lo_node
= o_xml_document->find_node( 'infCarga' ).
DATA: lo_kid   TYPE REF TO if_ixml_node.

DATA: lo_kid_1 TYPE REF TO if_ixml_node.

DATA: lv_name TYPE string,
      lv_value
TYPE string.
DATA: lv_node_type TYPE i.

lo_kid
= lo_node->get_first_child( ).
WHILE lo_kid IS NOT INITIAL.

 
IF lo_kid->get_type( ) EQ if_ixml_node=>co_node_element.
    lv_name
= lo_kid->get_name( ).
    lv_value
= lo_kid->get_value( ).

   
IF lv_name = 'infQ'.
      lo_kid_1
= lo_kid->get_first_child( ).

     
WHILE lo_kid_1 IS NOT INITIAL.

       
IF lo_kid_1->get_type( ) = if_ixml_node=>co_node_element.
          lv_name
= lo_kid_1->get_name( ).
          lv_value
= lo_kid_1->get_value( ).
         
WRITE: / lv_name, lv_value.
       
ENDIF.
        lo_kid_1
= lo_kid_1->get_next( ).
     
ENDWHILE.
   
ELSE.
     
WRITE: / lv_name.
     
WRITElv_value.
   
ENDIF.

 
ENDIF.
  lo_kid
= lo_kid->get_next( ).

ENDWHILE.

This sample XML


<?xml version="1.0" encoding="utf-8"?>

<infCarga>

  <vCarga>10000.00</vCarga>

  <proPred>PNEU</proPred>

  <xOutCat>GRANEL</xOutCat>

        <infQ>

         <cUnid>01</cUnid>

         <tpMed>PESO BRUTO</tpMed>

         <qCarga>13.0000</qCarga>

        </infQ>

        <infQ>

         <cUnid>03</cUnid>

         <tpMed>UNIDADE</tpMed>

         <qCarga>500.0000</qCarga>

        </infQ>

</infCarga>

Would generate this output


vCarga 10000.00

proPred PNEU

xOutCat GRANEL

cUnid 01

tpMed PESO BRUTO

qCarga 13.0000

cUnid 03

tpMed UNIDADE

qCarga 500.0000

Regards,
Naimesh Patel

3 REPLIES 3

Juwin
Active Contributor
0 Kudos

Hi Thiago,

I think you will have to get the children of that node (infCarga), loop through that and get the individual values .

Thanks,

Juwin

naimesh_patel
Active Contributor
0 Kudos

You can either create the simple transformation to convert the XML into your data structure. Or else, you would need to loop through the kids of the specific node.

DATAo_xml_document TYPE REF TO cl_xml_document.

DATA: v_tag_value TYPE string.
CREATE OBJECT o_xml_document TYPE cl_xml_document.

o_xml_document
->import_from_file(
filename
= 'C:\temp\test_file.xml').
DATA: lo_node TYPE REF TO if_ixml_node.

lo_node
= o_xml_document->find_node( 'infCarga' ).
DATA: lo_kid   TYPE REF TO if_ixml_node.

DATA: lo_kid_1 TYPE REF TO if_ixml_node.

DATA: lv_name TYPE string,
      lv_value
TYPE string.
DATA: lv_node_type TYPE i.

lo_kid
= lo_node->get_first_child( ).
WHILE lo_kid IS NOT INITIAL.

 
IF lo_kid->get_type( ) EQ if_ixml_node=>co_node_element.
    lv_name
= lo_kid->get_name( ).
    lv_value
= lo_kid->get_value( ).

   
IF lv_name = 'infQ'.
      lo_kid_1
= lo_kid->get_first_child( ).

     
WHILE lo_kid_1 IS NOT INITIAL.

       
IF lo_kid_1->get_type( ) = if_ixml_node=>co_node_element.
          lv_name
= lo_kid_1->get_name( ).
          lv_value
= lo_kid_1->get_value( ).
         
WRITE: / lv_name, lv_value.
       
ENDIF.
        lo_kid_1
= lo_kid_1->get_next( ).
     
ENDWHILE.
   
ELSE.
     
WRITE: / lv_name.
     
WRITElv_value.
   
ENDIF.

 
ENDIF.
  lo_kid
= lo_kid->get_next( ).

ENDWHILE.

This sample XML


<?xml version="1.0" encoding="utf-8"?>

<infCarga>

  <vCarga>10000.00</vCarga>

  <proPred>PNEU</proPred>

  <xOutCat>GRANEL</xOutCat>

        <infQ>

         <cUnid>01</cUnid>

         <tpMed>PESO BRUTO</tpMed>

         <qCarga>13.0000</qCarga>

        </infQ>

        <infQ>

         <cUnid>03</cUnid>

         <tpMed>UNIDADE</tpMed>

         <qCarga>500.0000</qCarga>

        </infQ>

</infCarga>

Would generate this output


vCarga 10000.00

proPred PNEU

xOutCat GRANEL

cUnid 01

tpMed PESO BRUTO

qCarga 13.0000

cUnid 03

tpMed UNIDADE

qCarga 500.0000

Regards,
Naimesh Patel

0 Kudos

, thank you very much...!

I was almost there, I forgot to mention that I was trying to use if_ixml_node_iterator and that was my mistake.

By the way, your blog/site Zevolving is very nice, well done...!