comp268 exercises

9
ABAP AND XML COMP268 Exercises / Solutions Stefan Bresch / SAP AG Thomas Ritter / SAP AG Christoph Wedler / SAP AG

Upload: koizak

Post on 29-Dec-2015

11 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: COMP268 Exercises

ABAP AND XML

COMP268

Exercises / Solutions

Stefan Bresch / SAP AGThomas Ritter / SAP AGChristoph Wedler / SAP AG

Page 2: COMP268 Exercises

2

Page 3: COMP268 Exercises

3

Page 4: COMP268 Exercises

4

General Remarks

To get features like code completion and ST debugging, you have to switch to the new debugger and the new editor: Go to transaction SE80, select “Utilities->Settings…” tab “ABAP editor”; check “Front End Editor (New) in tab “Editor” and “New Debugger” in tab “Debugging”.

Exercise 1 – XML Processing with SXML Reader

Exercise descriptionLearn how to parse an XML document using the SXML (dataset) reader. Go to transaction SE38 (or SE80), create a new program with type “Executable program”, proposed name ZCOMP268_SXML_<nr>, where <nr> is your group number.

Create an instance of the class CL_SXML_DATASET_READER. The xml document to be parsed is stored in file ZCOMP268.

Loop over all the nodes in the xml document using method NEXT_NODE. Stop if the final state IF_SXML_NODE=>CO_NT_FINAL is reached.

In this loop, determine the maximum depth of the documents by increasing a depth counter on open element and decreasing it on a close element.

Don’t forget to close the dataset reader at the end.

Additional / HintIf you are interested in displaying the XML document in your program, you can use method SHOW_XML of class ZCL_ABAP_BROWSER. The method expects an SXML reader instance. Create a dataset reader instance for file ZCOMP268.

Exercise 2 – ABAP Data Serialization with CALL TRANSFORMATION ID

Exercise descriptionLearn how to serialize and de-serialize ABAP data. Go to transaction SE38 (or SE80), create a new program with type “Executable program”, proposed name ZCOMP268_BXML_<nr>, where <nr> is your group number.

In this program, define a local class with a public attribute of elementary type. Don’t forget to add the interface IF_SERIALIZABLE_OBJECT. No implementation part is needed.

Create an instance of this class, and set the public attribute to some value. Serialize your instance using CALL TRANSFORMATION ID and result type XSTRING. Clear the reference variable. De-serialize the data using CALL TRANSFORMATION ID and source type XSTRING..

Additionally, learn how to use binary XML. Create a binary XML writer instance passing the constant IF_SXML=>CO_XT_BINARY to the factory method CREATE

of the class CL_SXML_STRING_WRITER. Serialize your instance using CALL TRANSFORMATION ID and the binary writer. Access the binary XML with writer method GET_OUTPUT. Create an XML reader instance of class CL_SXML_STRING_READER passing the binary data to the CREATE

method. The reader identifies the binary XML automatically De-serialize the binary data using CALL TRANSFORMATION ID and the binary reader.

Page 5: COMP268 Exercises

5

Exercise 3 – ABAP XML Mapping with Simple Transformations

Exercise descriptionLearn how to process an XML document using Simple Transformations. Go to transaction SE38 (or SE80), create a new program with type “Executable program”, proposed name ZCOMP268_ST_<nr>, where <nr> is your group number.

Go to transaction STRANS (or SE80), create a new transformation with type “Simple transformation“, proposed name ZCOMP268_ST_<nr>, where <nr> is your group number and activate the transformation.

In your ABAP program, use CALL TRANSFORMATION ID to create a simple XML document (XSTRING) by serializing an elementary data object or literal (integer, string or character). Use the binding name ROOT.

Now, de-serialize your data using the Simple Transformation ZCOMP268_ST_<nr>. In your Simple Transformation, you have to process the XML document, which is in the asXML format. The generated transformation data root is ROOT, don’t change it. Match the asXML envelope using literal XML content (Copy the envelope with the elements <asx:abap> and <asx:values> to the main template).

Match the <ROOT> element. Move the value of the <ROOT> element to the data root ROOT using the tt:value instruction.

Page 6: COMP268 Exercises

6

Proposed Solutions

Exercise 1 – XML Processing with SXML Reader

DATA reader TYPE REF TO if_sxml_reader.DATA max TYPE i.DATA depth TYPE i.

reader = cl_sxml_dataset_reader=>create( filename = 'ZCOMP268' ).reader->next_node( ).WHILE reader->node_type <> if_sxml_node=>co_nt_final.  CASE reader->node_type.    WHEN if_sxml_node=>co_nt_element_open.      depth = depth + 1.    WHEN if_sxml_node=>co_nt_element_close.      depth = depth - 1.  ENDCASE.  IF depth > max.    max = depth.  ENDIF.  reader->next_node( ).ENDWHILE.DATA dataset_reader TYPE REF TO cl_sxml_dataset_reader.dataset_reader ?= reader.dataset_reader->close( ).

Exercise 1 – XML Processing with SXML Reader –Additional / Hint

DATA dataset_reader TYPE REF TO cl_sxml_dataset_reader.dataset_reader ?= cl_sxml_dataset_reader=>create( filename = 'ZCOMP268' ).zcl_abap_browser=>show_xml( xml_reader = dataset_reader ).dataset_reader->close( ).

Exercise 2 – ABAP Data Serialization with CALL TRANSFORMATION ID

CLASS lcl DEFINITION.  PUBLIC SECTION.    INTERFACES: IF_SERIALIZABLE_OBJECT.    DATA i TYPE i.ENDCLASS.

START-OF-SELECTION.

  DATA my_lcl TYPE REF TO lcl.  CREATE OBJECT my_lcl.  my_lcl->i = 42.

  DATA xml TYPE xstring.  CALL TRANSFORMATION ID    SOURCE root = my_lcl    RESULT XML xml.

  CLEAR my_lcl.

  CALL TRANSFORMATION ID    SOURCE XML xml    RESULT root = my_lcl.

Exercise 2 – ABAP Data Serialization with CALL TRANSFORMATION ID and binary XML

  DATA writer TYPE REF TO cl_sxml_string_writer.  writer = cl_sxml_string_writer=>create( if_sxml=>co_xt_binary ).  CALL TRANSFORMATION ID    SOURCE root = my_lcl

Page 7: COMP268 Exercises

7

    RESULT XML writer.  xml = writer->get_output( ).

  DATA reader TYPE REF TO cl_sxml_string_reader.  reader ?= cl_sxml_string_reader=>create( xml ).  CALL TRANSFORMATION ID    SOURCE XML reader    RESULT root = my_lcl.

Exercise 3 – ABAP XML Mapping with Simple Transformations - ABAP

DATA xml TYPE xstring.  CALL TRANSFORMATION ID  SOURCE root = 42  RESULT XML xml.

DATA i TYPE i.CALL TRANSFORMATION zcomp268_st_00  SOURCE XML xml  RESULT root = i.

Exercise 3 – ABAP XML Mapping with Simple Transformations - ST

<?sap.transform simple?><tt:transform xmlns:tt="http://www.sap.com/transformation-templates">

  <tt:root name="ROOT"/>

  <tt:template>    <asx:abap xmlns:asx="http://www.sap.com/abapxml">      <asx:values>        <ROOT>          <tt:value ref="ROOT"/>        </ROOT>      </asx:values>    </asx:abap>  </tt:template>

</tt:transform>

Page 8: COMP268 Exercises

8

Copyright 2007 SAP AG. All Rights Reserved No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP

AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software

vendors. Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation. IBM, DB2, DB2 Universal Database, OS/2, Parallel Sysplex, MVS/ESA, AIX, S/390, AS/400, OS/390, OS/400, iSeries, pSeries,

xSeries, zSeries, System i, System i5, System p, System p5, System x, System z, System z9, z/OS, AFP, Intelligent Miner, WebSphere, Netfinity, Tivoli, Informix, i5/OS, POWER, POWER5, POWER5+, OpenPower and PowerPC are trademarks or registered trademarks of IBM Corporation.

Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other countries.

Oracle is a registered trademark of Oracle Corporation. UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks

of Citrix Systems, Inc. HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C®, World Wide Web Consortium, Massachusetts

Institute of Technology. Java is a registered trademark of Sun Microsystems, Inc. JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by

Netscape. MaxDB is a trademark of MySQL AB, Sweden. SAP, R/3, mySAP, mySAP.com, xApps, xApp, SAP NetWeaver, and other SAP products and services mentioned herein as well

as their respective logos are trademarks or registered trademarks of SAP AG in Germany and in several other countries all over the world. All other product and service names mentioned are the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary.

The information in this document is proprietary to SAP. No part of this document may be reproduced, copied, or transmitted in any form or for any purpose without the express prior written permission of SAP AG.

This document is a preliminary version and not subject to your license agreement or any other agreement with SAP. This document contains only intended strategies, developments, and functionalities of the SAP® product and is not intended to be binding upon SAP to any particular course of business, product strategy, and/or development. Please note that this document is subject to change and may be changed by SAP at any time without notice.

SAP assumes no responsibility for errors or omissions in this document. SAP does not warrant the accuracy or scompleteness of the information, text, graphics, links, or other items contained within this material. This document is provided without a warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability, fitness for a particular purpose, or non-infringement.

SAP shall have no liability for damages of any kind including without limitation direct, special, indirect, or consequential damages that may result from the use of these materials. This limitation shall not apply in cases of intent or gross negligence.

The statutory liability for personal injury and defective products is not affected. SAP has no control over the information that you may access through the use of hot links contained in these materials and does not endorse your use of third-party Web pages nor provide any warranty whatsoever relating to third-party Web pages.

SAP assumes no responsibility for errors or omissions in these materials