*&---------------------------------------------------------------------*
*& 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输出的更多相关文章

  1. ABAP内表数据和JSON格式互转

    本程序演示ABAP内表数据如何转为JSON格式,以及JSON数据如何放入内表. 注:json字符串格式如:jsonstr = '[ {flag: "0",message: &quo ...

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

    内表: A B C IMINGZHA  HAIMINGZ AIMINGZH 1 2 3 4 5 6 Json串:  [{a: "IMINGZHA", b: "HAIMIN ...

  3. ABAP 内表 详解

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. Abap 内表的语法

    ABAP中的内表相当于其他程序设计语言中的二维数组,存储多行结构相同的数据 不同于二维数组,内表在创建后,列结构与列数是固定不变的,而行数是动态增长的  内表支持循环对每行数据进行操作,也支持整体操作 ...

  5. REUSE_ALV_GRID_DISPLAY显示ALV,设置可编辑时,与内表数据同步问题

    使用function module: REUSE_ALV_GRID_DISPLAY显示ALV,并设置alv某些列可编辑,可是编辑后发现对应的内表数据并没有随之改变.记得需要设置一个参数的值,怎么想也记 ...

  6. ABAP 内表与XML转换

    1需求说明 在系统交互中需要将SAP内表转换为XML文件,发送给其他系统,并且将其他系统返回的XML文件转换为SAP内表. 2创建转换编辑器 事务代码:STRANS 选择简单转换 以图形方式编辑 右键 ...

  7. ABAP 内表的行列转换-发货通知单-打印到Excel里-NEW-(以运单号为单位显示ALV然后保存输出)

    *********************************************************************** * Title           : ZSDF003  ...

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

    *********************************************************************** * Title           : ZSDF002  ...

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

    需要传入数据到Excel里的模板如上图所示 ********************** *           设计主要逻辑与原理说明                                 ...

随机推荐

  1. call、aply、bind的常用方法总结

    类函数变为数组 function aaa (){ Array.prototype.slice(arguments); } 进一步操作它的每个元素 function bbb() { Array.prot ...

  2. InnoDB事务和锁

    InnoDB支持事务,MyISAM不支持事务. 一.事务的基本特性 ACID特性 1.原子性(Atomicity):事务是一个原子操作单元,其对数据的修改,要么全都执行,要么全都不执行. 2.一致性( ...

  3. Python3的bytes/str之别

    Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分.文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示.Python 3不会以任意隐式的方式混用str ...

  4. bc的用法

    bc是An arbitrary precision calculator language,即一个任意精度的计算语言,注意是一种语言,它提供了一些语法结构,比如条件判断.循环等,可以说是很强大的,但是 ...

  5. 【HTML5】元素<head>的使用

    功能描述 在新建的页面<head>元素中,加入该元素所包含的各类标签,并定义超级链接的样式.当单击"请点击我"标签时,并展示相应效果并进入<base>元素设 ...

  6. java在linux上始终无法用jdbc跟myql连接

    确实手动在机器上连接mysql没问题的话,尝试下面的方法   a.重启网卡 b.重启系统

  7. oc 经常用到弹出view的方法

    #pragma mark 弹出view -(void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur { CAKeyframeA ...

  8. NB的CSS样式集锦1——CSS3滚动条美化,CSS3滚动条皮肤

    转自:http://www.pengyaou.com/codecss3/POKDNMS_112.html CSS3 -webkit-scrollbar滚动条皮肤美化实现,利用-webkit-scrol ...

  9. Quartus和ISE调用Synplify进行综合的问题

    分别尝试采用Quartus和ISE调用第三方综合软件Synplify进行综合. [软件版本] Quartus II 13.0 (SP).ISE 14.4 .Synplify 201303. [问题描述 ...

  10. 自定义type='file'上传文件样式

    改变默认的上传文件样式: 用label作为替代 <input id="file_-1" type="file" name="file" ...