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

Reading XML with ABAP

Former Member
0 Likes
2,196

I've got the XML file (below; second snippet) loaded into memory. However, the call to l_rif_document->find_from_path_ns( ) at the bottom of the first snippet returns nothing. What must I do to return a reference to the root node so I can then loop the child nodes and get the data I need?

data: l_rif_ixml      type ref to if_ixml,
      l_rif_factory   type ref to if_ixml_stream_factory,
      l_rif_parser    type ref to if_ixml_parser,
      l_rif_istream   type ref to if_ixml_istream,
      l_rif_document  type ref to if_ixml_document,
      l_rif_node      type ref to if_ixml_node,
      l_rif_element   type ref to if_ixml_element.

  " Create central factor object
  l_rif_ixml = cl_ixml=>create( ).

  " Create factory object for the data stream
  l_rif_factory = l_rif_ixml->create_stream_factory( ).

  " Create input stream
  l_rif_istream = l_rif_factory->create_istream_string( string = p_data_in ).

  " Create document
  l_rif_document = l_rif_ixml->create_document( ).

  " Create XML parser
  l_rif_parser = l_rif_ixml->create_parser( document        = l_rif_document
                                            stream_factory  = l_rif_factory
                                            istream         = l_rif_istream ).

  " Parse input stream
  l_rif_parser->parse( ).

  l_rif_element = l_rif_document->find_from_path_ns( default_uri = space*
                                                     *path = '/MYREPORT' ).

And this is the XML document:

<MYREPORT>
	<RECORD>
	  <A-CARRIER-NAME>ACME HEALTH PLAN</A-CARRIER-NAME> 
	  <B-CARRIER-CODE>DAI1</B-CARRIER-CODE> 
	  <C-STATUS>Active Billing</C-STATUS> 
	  <D-TYPE>1-PARTICIPANT</D-TYPE> 
	  <E-NAME>DOE, JOHN</E-NAME> 
	  <F-STREET-ADDRESS>100 MAIN STREET</F-STREET-ADDRESS> 
	  <G-CITY>WESTON</G-CITY>                                                         
	  <H-STATE>KY</H-STATE> 
	  <I-ZIP>90210</I-ZIP> 
	  <J-EMPLOYEE-ID /> 
	  <K-SSN></K-SSN> 
	  <L-ESSN></L-ESSN> 
	  <M-DOB>01-01-45</M-DOB> 
	  <N-SEX>M</N-SEX> 
	  <O-RELT>EMP</O-RELT> 
	  <P-COVERAGE-TYPE>Medical</P-COVERAGE-TYPE> 
	  <Q-OPTION-CODE>A</Q-OPTION-CODE> 
	  <R-FAMILY-STATUS>3</R-FAMILY-STATUS> 
	  <S-COBRA-CODE>C</S-COBRA-CODE> 
	  <T-QE-DATE>01-31-08</T-QE-DATE> 
	  <U-BENTERM-DATE>02-29-08</U-BENTERM-DATE> 
	  <V-COBRA-ELIG-STARTS>03-01-08</V-COBRA-ELIG-STARTS> 
	  <W-COBRA-ELIG-ENDS>08-31-09</W-COBRA-ELIG-ENDS> 
	  <X-PAID-THRU>05-31-08</X-PAID-THRU> 
	  <Y-GRACE-ENDING>07-02-08</Y-GRACE-ENDING> 
	  <ZA-DIV>A1</ZA-DIV> 
	  <ZB-UNIT>41</ZB-UNIT> 
	  <ZC-CHANGE /> 
	  <ZD-FROM /> 
	  <ZE-TO /> 
	  <ZF-DOCID>43535234</ZF-DOCID> 
	  <ZG-BENEFITS-CLASS /> 
	</RECORD>
</MYREPORT>

I've got the XML file (below; second snippet) loaded into memory. However, the call to l_rif_document->find_from_path_ns( ) at the bottom of the first snippet returns nothing. What must I do to return a reference to the root node so I can then loop the child nodes and get the data I need?

data: l_rif_ixml      type ref to if_ixml,
      l_rif_factory   type ref to if_ixml_stream_factory,
      l_rif_parser    type ref to if_ixml_parser,
      l_rif_istream   type ref to if_ixml_istream,
      l_rif_document  type ref to if_ixml_document,
      l_rif_node      type ref to if_ixml_node,
      l_rif_element   type ref to if_ixml_element.

  " Create central factor object
  l_rif_ixml = cl_ixml=>create( ).

  " Create factory object for the data stream
  l_rif_factory = l_rif_ixml->create_stream_factory( ).

  " Create input stream
  l_rif_istream = l_rif_factory->create_istream_string( string = p_data_in ).

  " Create document
  l_rif_document = l_rif_ixml->create_document( ).

  " Create XML parser
  l_rif_parser = l_rif_ixml->create_parser( document        = l_rif_document
                                            stream_factory  = l_rif_factory
                                            istream         = l_rif_istream ).

  " Parse input stream
  l_rif_parser->parse( ).

  l_rif_element = l_rif_document->find_from_path_ns( default_uri = space*
                                                     *path = '/MYREPORT' ).

And this is the XML document:

<MYREPORT>
	<RECORD>
	  <A-CARRIER-NAME>ACME HEALTH PLAN</A-CARRIER-NAME> 
	  <B-CARRIER-CODE>DAI1</B-CARRIER-CODE> 
	  <C-STATUS>Active Billing</C-STATUS> 
	  <D-TYPE>1-PARTICIPANT</D-TYPE> 
	  <E-NAME>DOE, JOHN</E-NAME> 
	  <F-STREET-ADDRESS>100 MAIN STREET</F-STREET-ADDRESS> 
	  <G-CITY>WESTON</G-CITY>                                                         
	  <H-STATE>KY</H-STATE> 
	  <I-ZIP>90210</I-ZIP> 
	  <J-EMPLOYEE-ID /> 
	  <K-SSN></K-SSN> 
	  <L-ESSN></L-ESSN> 
	  <M-DOB>01-01-45</M-DOB> 
	  <N-SEX>M</N-SEX> 
	  <O-RELT>EMP</O-RELT> 
	  <P-COVERAGE-TYPE>Medical</P-COVERAGE-TYPE> 
	  <Q-OPTION-CODE>A</Q-OPTION-CODE> 
	  <R-FAMILY-STATUS>3</R-FAMILY-STATUS> 
	  <S-COBRA-CODE>C</S-COBRA-CODE> 
	  <T-QE-DATE>01-31-08</T-QE-DATE> 
	  <U-BENTERM-DATE>02-29-08</U-BENTERM-DATE> 
	  <V-COBRA-ELIG-STARTS>03-01-08</V-COBRA-ELIG-STARTS> 
	  <W-COBRA-ELIG-ENDS>08-31-09</W-COBRA-ELIG-ENDS> 
	  <X-PAID-THRU>05-31-08</X-PAID-THRU> 
	  <Y-GRACE-ENDING>07-02-08</Y-GRACE-ENDING> 
	  <ZA-DIV>A1</ZA-DIV> 
	  <ZB-UNIT>41</ZB-UNIT> 
	  <ZC-CHANGE /> 
	  <ZD-FROM /> 
	  <ZE-TO /> 
	  <ZF-DOCID>43535234</ZF-DOCID> 
	  <ZG-BENEFITS-CLASS /> 
	</RECORD>
</MYREPORT>

2 REPLIES 2
Read only

BGarcia
Active Contributor
0 Likes
1,003

Hello Steve,

At first glance it seems all OK. So I've tried your code, and put some contents of your XML elements to a string variable. I've executed it and l_rif_element variable was correctly bound.

Here's your example, but declaring some XML elements inside the code:


data: l_rif_ixml      type ref to if_ixml,
      l_rif_factory   type ref to if_ixml_stream_factory,
      l_rif_parser    type ref to if_ixml_parser,
      l_rif_istream   type ref to if_ixml_istream,
      l_rif_document  type ref to if_ixml_document,
      l_rif_node      type ref to if_ixml_node,
      l_rif_element   type ref to if_ixml_element.
DATA: lv_string       TYPE string.

  lv_string = '<MYREPORT>'.
  CONCATENATE lv_string '<RECORD>' INTO lv_string SEPARATED BY SPACE.
  CONCATENATE lv_string '<A-CARRIER-NAME>ACME HEALTH PLAN</A-CARRIER-NAME>' INTO lv_string SEPARATED BY SPACE.
  CONCATENATE lv_string '<B-CARRIER-CODE>DAI1</B-CARRIER-CODE>' INTO lv_string SEPARATED BY SPACE.
  CONCATENATE lv_string '<C-STATUS>Active Billing</C-STATUS>' INTO lv_string SEPARATED BY SPACE.
  CONCATENATE lv_string '<J-EMPLOYEE-ID />' INTO lv_string SEPARATED BY SPACE.
  CONCATENATE lv_string '<K-SSN></K-SSN> ' INTO lv_string SEPARATED BY SPACE.
  CONCATENATE lv_string '</RECORD>' INTO lv_string SEPARATED BY SPACE.
  CONCATENATE lv_string '</MYREPORT>' INTO lv_string SEPARATED BY SPACE.

  " Create central factor object
  l_rif_ixml = cl_ixml=>create( ).

  " Create factory object for the data stream
  l_rif_factory = l_rif_ixml->create_stream_factory( ).

  " Create input stream
  l_rif_istream = l_rif_factory->create_istream_string( string = lv_string ).

  " Create document
  l_rif_document = l_rif_ixml->create_document( ).

  " Create XML parser
  l_rif_parser = l_rif_ixml->create_parser( document        = l_rif_document
                                            stream_factory  = l_rif_factory
                                            istream         = l_rif_istream ).

  " Parse input stream
  l_rif_parser->parse( ).

  l_rif_element = l_rif_document->find_from_path_ns( default_uri = space
                                                     path = '/MYREPORT' ).
  lv_string = l_rif_element->get_name( ).

Kind regards.

Bruno

Read only

Former Member
0 Likes
1,003

Curious. Something must be wrong with how I'm loading the XML file.

In frustration I switched to another approach: using 'SCMS_STRING_TO_XSTRING' and 'SMUM_XML_PARSE'. This is working. Go figure.