ABAP内表数据做层次XML输出

*&---------------------------------------------------------------------*
*& Report  Z_BARRY_TEST_XML
*&---------------------------------------------------------------------*
*& 46C - ECC6 通用
*&---------------------------------------------------------------------*
REPORT  z_barry_test_xml.

TYPES: BEGIN OF ty_item ,
         ebelp LIKE ekpo-ebelp ,
         menge LIKE ekpo-menge ,
       END OF ty_item .
TYPES tt_item TYPE STANDARD TABLE OF ty_item WITH DEFAULT KEY  .
DATA: itab_item TYPE STANDARD TABLE OF ty_item WITH HEADER LINE  ,
      wa_item LIKE LINE OF itab_item.

TYPES: BEGIN OF ty_po ,
         ebeln LIKE ekko-ebeln ,
         aedat LIKE ekko-aedat ,
         item TYPE tt_item ,
       END OF ty_po .
DATA: itab_po TYPE STANDARD TABLE OF ty_po WITH HEADER LINE  .

DATA: BEGIN OF itab_all OCCURS 0 ,
        ebeln LIKE ekko-ebeln ,
        aedat LIKE ekko-aedat ,
        ebelp LIKE ekpo-ebelp ,
        menge LIKE ekpo-menge ,
      END OF itab_all.

DATA: tmpebeln LIKE ekko-ebeln.

DATA: l_ixml TYPE REF TO if_ixml,
      l_encoding TYPE REF TO if_ixml_encoding,
      l_comment TYPE REF TO if_ixml_comment,
      l_doc TYPE REF TO if_ixml_document,
      l_root TYPE REF TO if_ixml_element,
      l_po   TYPE REF TO if_ixml_element,
      l_pohead TYPE REF TO if_ixml_element,
      l_item TYPE REF TO if_ixml_element,
      l_element TYPE REF TO if_ixml_element,
      l_attribute TYPE REF TO if_ixml_attribute,
      l_ostream TYPE REF TO if_ixml_ostream,
      l_factory  TYPE REF TO if_ixml_stream_factory.
TYPES: BEGIN OF xml_line,
        data(256) TYPE x,
      END OF xml_line.
DATA: xml_table TYPE TABLE OF xml_line,
      xml_size  TYPE i.
DATA: l_rval TYPE i.
DATA: str TYPE string .

SELECT-OPTIONS : s_ebeln FOR itab_all-ebeln.

START-OF-SELECTION.
  PERFORM getdata.
  PERFORM create_xml.
  PERFORM download.
*---------------------------------------------------------------------*
*       FORM getdata                                                  *
*---------------------------------------------------------------------*
FORM getdata.
  SELECT ekko~ebeln ekko~aedat ekpo~ebelp ekpo~menge
  INTO TABLE itab_all
  FROM ekko INNER JOIN ekpo ON ekko~ebeln = ekpo~ebeln
  WHERE ekko~ebeln IN s_ebeln.

SORT itab_all BY ebeln ebelp.

LOOP AT itab_all .
    ON CHANGE OF itab_all-ebeln .
      CLEAR itab_po-item .
      CLEAR tmpebeln .
      tmpebeln = itab_all-ebeln .
      LOOP AT itab_all WHERE ebeln = tmpebeln .
        wa_item-ebelp = itab_all-ebelp .
        wa_item-menge = itab_all-menge .
        APPEND wa_item TO itab_po-item .
      ENDLOOP .
      SORT itab_po-item BY ebelp.
      itab_po-ebeln = itab_all-ebeln .
      itab_po-aedat = itab_all-aedat .
      APPEND itab_po .
    ENDON .
  ENDLOOP .
ENDFORM.                    "getdata
*---------------------------------------------------------------------*
*       FORM create_xml                                               *
*---------------------------------------------------------------------*
FORM create_xml.
  CLASS cl_ixml DEFINITION LOAD.
  l_ixml = cl_ixml=>create( ).
  CALL METHOD l_ixml->create_encoding
    EXPORTING
      byte_order    = 0
      character_set = ‘gb2312‘
    RECEIVING
      rval          = l_encoding.
  CALL METHOD l_ixml->create_document
    RECEIVING
      rval = l_doc.
  CALL METHOD l_doc->set_encoding
    EXPORTING
      encoding = l_encoding.
  CALL METHOD l_doc->create_simple_element
    EXPORTING
      name   = ‘Root‘
      parent = l_doc
    RECEIVING
      rval   = l_root.
  CALL METHOD l_root->set_attribute
    EXPORTING
      name  = ‘ATTRIBUTE‘
      value = ‘sample‘
    RECEIVING
      rval  = l_rval.
  CALL METHOD l_doc->create_comment
    EXPORTING
      comment = ‘‘‘上面ATTRIBUTE为属性示例‘‘‘
    RECEIVING
      rval    = l_comment.
  CALL METHOD l_root->append_child
    EXPORTING
      new_child = l_comment
    RECEIVING
      rval      = l_rval.

LOOP AT itab_po.

CALL METHOD l_doc->create_simple_element
      EXPORTING
        name   = ‘po‘
        parent = l_root
      RECEIVING
        rval   = l_po.

CALL METHOD l_doc->create_simple_element
    EXPORTING
      name   = ‘pohead‘
      parent = l_po
    RECEIVING
      rval   = l_pohead.

str = itab_po-ebeln .
    CALL METHOD l_doc->create_simple_element
      EXPORTING
        name   = ‘ponumber‘
        parent = l_pohead
        value  = str
      RECEIVING
        rval   = l_element.
    str = itab_po-aedat .
    CALL METHOD l_doc->create_simple_element
      EXPORTING
        name   = ‘podate‘
        parent = l_pohead
        value  = str
      RECEIVING
        rval   = l_element.

PERFORM fill_itab2xml TABLES  itab_po-item
                          USING   ‘pobody‘
                                  ‘poitem‘
                                  l_po.
  ENDLOOP.

CALL METHOD l_ixml->create_stream_factory
    RECEIVING
      rval = l_factory.
  CALL METHOD l_factory->create_ostream_itable
    EXPORTING
      table = xml_table
    RECEIVING
      rval  = l_ostream.
  CALL METHOD l_doc->render
    EXPORTING
      ostream   = l_ostream
      recursive = ‘X‘.
  CALL METHOD l_ostream->get_num_written_raw
    RECEIVING
      rval = xml_size.
ENDFORM.                    "create_xml

*---------------------------------------------------------------------*
*       FORM fill_itab                                                *
*---------------------------------------------------------------------*
FORM fill_itab2xml TABLES intab
                   USING node1name TYPE string
                         node2name TYPE string
                         l_parent TYPE REF TO if_ixml_element.
  DATA: BEGIN OF headtab OCCURS 0 ,
          length    TYPE i ,
          decimals  TYPE i,
          type_kind TYPE c,
          name(30)  TYPE c,
        END OF headtab.
  DATA descr_ref TYPE REF TO cl_abap_structdescr.
  FIELD-SYMBOLS: <comp_wa> TYPE abap_compdescr ,
                 <f_field> ,
                 <f_intab> TYPE ANY .
  DATA: n TYPE i ,
        str TYPE string ,
        itemname TYPE string ,
        text1 TYPE c ,
        l_node TYPE REF TO if_ixml_element ,
        l_item TYPE REF TO if_ixml_element .
  descr_ref ?= cl_abap_typedescr=>describe_by_data( intab ).
  LOOP AT descr_ref->components ASSIGNING <comp_wa>.
    MOVE-CORRESPONDING <comp_wa> TO headtab.
    APPEND headtab.
  ENDLOOP.
  CALL METHOD l_doc->create_simple_element
    EXPORTING
      name   = node1name
      parent = l_parent
    RECEIVING
      rval   = l_node.
  DESCRIBE TABLE headtab LINES n.
  LOOP AT intab ASSIGNING <f_intab>.
    CALL METHOD l_doc->create_simple_element
      EXPORTING
        name   = node2name
        parent = l_node
      RECEIVING
        rval   = l_item.
    DO n TIMES.
      ASSIGN COMPONENT sy-index OF STRUCTURE <f_intab> TO <f_field>.
      str = <f_field>.
      READ TABLE headtab INDEX sy-index.
      IF headtab-type_kind = ‘I‘ OR headtab-type_kind = ‘P‘
                                 OR headtab-type_kind = ‘F‘.
        SEARCH str FOR ‘-‘.
        IF sy-subrc = 0 AND sy-fdpos <> 0.
          SPLIT str AT ‘-‘ INTO str text1.
          CONDENSE str.
          CONCATENATE ‘-‘ str INTO str.
        ELSE.
          CONDENSE str.
        ENDIF.
      ELSE.
*        SHIFT str LEFT DELETING LEADING ‘0‘ .
      ENDIF.
      itemname = headtab-name .
      CALL METHOD l_doc->create_simple_element
        EXPORTING
          name   = itemname
          parent = l_item
          value  = str
        RECEIVING
          rval   = l_element.
    ENDDO.
  ENDLOOP.
ENDFORM.                    "fill_itab2xml

*---------------------------------------------------------------------*
*       FORM download                                                 *
*---------------------------------------------------------------------*
FORM download.
  CALL FUNCTION ‘GUI_DOWNLOAD‘
       EXPORTING
            bin_filesize = xml_size
            filename     = ‘C:\46C.XML‘
            filetype     = ‘BIN‘
       TABLES
            data_tab     = xml_table.
ENDFORM.                    "download

稍微修改一下,就能达到下图的效果:

ABAP内表数据做层次XML输出,布布扣,bubuko.com

时间: 2024-10-12 20:13:11

ABAP内表数据做层次XML输出的相关文章

ABAP 内表数据 与 Json串 相互转换

DATA: json_ser TYPE REF TO CL_TREX_JSON_SERIALIZER, json_des TYPE REF TO CL_TREX_JSON_DESERIALIZER. DATA: jsonstr TYPE string. CREATE OBJECT json_ser EXPORTING data = itab[]. CALL METHOD json_ser->SERIALIZE. CALL METHOD json_ser->GET_DATA RECEIVING

ABAP 内表 详解

声明:原创作品,转载时请注明文章来自SAP师太技术博客:www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将追究法律责任!原文链接:http://www.cnblogs.com/jiangzhengjun/p/4293475.html 老式的内表类型定义... 214 基于基本类型内表类型... 214 基于结构体类型内表类型... 215 老式的内表对象创建... 215 参照现有内表类型或内表对象来创建... 215 参照现有结构类型或结构对象来

修改内表数据并输出结果

*定义结构ty_sales TYPES:BEGIN OF ty_sales, customerid(3) TYPE n, productid(3) TYPE n, orderid(3) TYPE n, customername(10) TYPE c, amount TYPE i, END OF ty_sales. *定义内表和工作区 DATA: it_sales TYPE STANDARD TABLE OF ty_sales WITH KEY customerid productid order

创建内表并输出内表数据

*Deal Data with Internal Table *从表scarr里面读取信息 *用scarr定义一个结构 DATA wa_scarr TYPE scarr. *定义内表 DATA it_scarr TYPE TABLE OF scarr. *选择数据 SELECT * INTO CORRESPONDING FIELDS OF TABLE it_scarr FROM scarr. *检查数据 IF sy-subrc = 0. *遍历内表 LOOP AT it_scarr INTO w

ABAP 内表的行列转换-发货通知单-打印到Excel里

需要传入数据到Excel里的模板如上图所示 *&---------------------------------------------------------------------* *& Report  Z_TEST_COL_TO_ROW_02 *& *&---------------------------------------------------------------------* *& *& *&----------------

ABAP 内表的行列转换-发货通知单2

*&---------------------------------------------------------------------* *& Report  Z_TEST_COL_TO_ROW_02 *& *&---------------------------------------------------------------------* *& *& *&--------------------------------------

ABAP 内表访问表达式的性能

内表访问表达式是ABAP 7.4中引入的重要特性,可以使语句变得更加简洁.美观.那么它的读写性能怎么样呢?我进行了一点点测试. 读取 测试代码,使用三种方式读取同一内表,分别是read table关键字.表达式和read table...binary search: REPORT ztest_read. DATA: BEGIN OF gs_test, index TYPE i, des TYPE string, END OF gs_test. DATA: gt_test LIKE STANDAR

ABAP 内表

定义内表 1. 先声明表结构, 再根据表结构定义内表. TYPES: BEGIN OF w_itab, a(10), b(10), END OF w_itab. DATA: itab1 type standard table of w_itab with header line. DATA: itab2 like table of itab1. 2. 先声明工作区, 再引用工作区结构来声明一个内表. DATA: begin of w_itab, a(10), b(10), end of w_it

事务代码STRANS的使用(XML与ABAP内表相互转换)

ABAP ---> XML  and XML --->ABAP 要求的XML格式如下: <handleMLPurchaseOrderServiceResponse> <out> <headers> <baseReturnHeader> <hresv1>4500524641</hresv1> <hresv2/> <hresv3/> <hresv4/> <hresv5/> <