17/09/2010

W3C DOM and the SAP iXML Library


REPORT ZADN_SAMPLE.

*----------------------------------------------------------------------*
* -> Local Class Definitions *
*----------------------------------------------------------------------*

CLASS lcl_dom_xmlprocessor DEFINITION.

PUBLIC SECTION.

CLASS-METHODS : class_constructor,
main.

METHODS : constructor,
add_partner IMPORTING im_id TYPE string
im_type TYPE i
im_name TYPE string
im_date TYPE d,
show_document.

PRIVATE SECTION.

CLASS-DATA : ixml_factory TYPE REF TO if_ixml.

DATA : partners_doc TYPE REF TO if_ixml_document,
partners_node TYPE REF TO if_ixml_element.

ENDCLASS. "lcl_dom_xmlprocessor DEFINITION

*----------------------------------------------------------------------*
* - > CLASS lcl_dom_xmlprocessor IMPLEMENTATION *
*----------------------------------------------------------------------*
* - > LOcal Class Implementation *
*----------------------------------------------------------------------*
CLASS lcl_dom_xmlprocessor IMPLEMENTATION.

METHOD class_constructor.

* Retrieve a reference to the iXML factory:
ixml_factory = cl_ixml=>create( ).

ENDMETHOD. "class_constructor

METHOD constructor.

* Create a new DOM-based XML document:
partners_doc = ixml_factory->create_document( ).

* Create the root "partners" element:
partners_node =
partners_doc->create_simple_element(
name = 'Partners'
parent = partners_doc ).

ENDMETHOD. "constructor

METHOD main.

* Method-Local Data Declarations:
DATA: lo_dom_processor TYPE REF TO lcl_dom_xmlprocessor.

* Create an instance of the test driver class:
CREATE OBJECT lo_dom_processor.

* Add a couple of partners to the list:
lo_dom_processor->add_partner( im_id = '12345'
im_type = 2
im_name = 'SAP Developer Network, SDN.'
im_date = '20060201' ).

lo_dom_processor->add_partner( im_id = '33589'
im_type = 1
im_name = 'ABAP Developer Network, Brasil.'
im_date = '20090911' ).

lo_dom_processor->add_partner( im_id = '23456'
im_type = 2
im_name = 'SAP Press, Books Online.'
im_date = '20090615' ).

* Display the resultant XML document on the screen:
lo_dom_processor->show_document( ).

ENDMETHOD. "main

METHOD add_partner.

* Method-Local Data Declarations:
DATA: lo_partner_node TYPE REF TO if_ixml_element,
lv_type TYPE string,
lv_creation_date TYPE string.

* Copy the parameters into string format:
lv_type = im_type.

CONCATENATE im_date+0(4) im_date+4(2) im_date+6(2) INTO lv_creation_date SEPARATED BY '-'.

* Create a partner node:
lo_partner_node = partners_doc->create_simple_element( name = 'Partner'
parent = partners_node ).

* Set the "type" attribute on the partners:
lo_partner_node->set_attribute( name = 'Type' value = lv_type ).

* Fill in the remaining information for the partner:
partners_doc->create_simple_element( name = 'PartnerId'
value = im_id
parent = lo_partner_node ).

partners_doc->create_simple_element( name = 'Name'
value = im_name
parent = lo_partner_node ).

partners_doc->create_simple_element( name = 'CreationDate'
value = lv_creation_date
parent = lo_partner_node ).

ENDMETHOD. "add_partner

METHOD show_document.

* Display the XML document on the screen:
CALL FUNCTION 'SDIXML_DOM_TO_SCREEN'
EXPORTING
document = partners_doc
EXCEPTIONS
no_document = 1
OTHERS = 2.

ENDMETHOD. "show_document

ENDCLASS. "lcl_dom_xmlprocessor IMPLEMENTATION

*----------------------------------------------------------------------*
* -> START-OF-SELECTION Event Module *
*----------------------------------------------------------------------*

START-OF-SELECTION.
lcl_dom_xmlprocessor=>main( ).



Source: SAP

Nenhum comentário:

Postar um comentário