Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7184803
  • 博文数量: 655
  • 博客积分: 10264
  • 博客等级: 上将
  • 技术积分: 8278
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-04 17:47
个人简介

ABAP顾问

文章分类

全部博文(655)

文章存档

2017年(2)

2014年(8)

2013年(3)

2012年(2)

2011年(18)

2010年(102)

2009年(137)

2008年(274)

2007年(134)

分类:

2008-01-20 21:49:53

今天有个兄弟问如何实现以XML的方式输出内表的内容,这个问题我以前好像没有写过。倒不是不会写,而是写的方法太多了,有极其简单的,也有很复杂的,而且网上资料也很多。
找到以前写的一个程序,稍微修改了一下,能实现一般的功能。
 
*&---------------------------------------------------------------------*
*& Report  Z_BARRY_TEST_XML
*&
*&---------------------------------------------------------------------*
*& 46C - ECC6 通用
*&---------------------------------------------------------------------*
REPORT  z_barry_test_xml.
DATA: BEGIN OF itab_matnr OCCURS 0 ,
        matnr LIKE mara-matnr ,
        maktx LIKE makt-maktx ,
      END OF itab_matnr .
DATA: BEGIN OF itab_class OCCURS 0 ,
        class LIKE m_wwgha-class,"部门
        kschl LIKE m_wwgha-kschl,"DESC
      END OF itab_class .
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_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.
START-OF-SELECTION.
  PERFORM getdata.
  PERFORM create_xml.
  PERFORM download.
*---------------------------------------------------------------------*
*       FORM getdata                                                  *
*---------------------------------------------------------------------*
FORM getdata.
  SELECT matnr maktx FROM makt
  INTO TABLE itab_matnr
  UP TO 4 ROWS
  WHERE spras = '1' .
  SELECT class kschl FROM m_wwgha
  INTO TABLE itab_class
  UP TO 4 ROWS
  WHERE spras IN ('1','E') .
  DELETE itab_class WHERE class = '0'.
  APPEND itab_class. "演示空数据
ENDFORM.
*---------------------------------------------------------------------*
*       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.
  PERFORM fill_itab2xml TABLES  itab_matnr
                        USING   'Products'
                                'Product'
                                l_root.
  PERFORM fill_itab2xml TABLES  itab_class
                        USING   'Categories'
                                'Categorie'
                                l_root.
  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.
*---------------------------------------------------------------------*
*       FORM download                                                 *
*---------------------------------------------------------------------*
FORM download.
  CALL FUNCTION 'GUI_DOWNLOAD'
       EXPORTING
            bin_filesize = xml_size
            filename     = 'C:\ECC6.XML'
            filetype     = 'BIN'
       TABLES
            data_tab     = xml_table.
ENDFORM.
*---------------------------------------------------------------------*
*       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: TYPE abap_compdescr ,
                 ,
                 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 .
    MOVE-CORRESPONDING 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 .
    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 TO .
      str = .
      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.
 
如果是ECC 5.0或者以后的系统,推荐使用Simple Transformations:
 
关于Simple Transformation的例子,如果你是ECC6,可以参考一下程序:SSTDEMO_FLIGHTS和STRANSDEMO_FLIGHTS
或者看例程:http://blog.chinaunix.net/u1/40527/showart_495514.html
阅读(7537) | 评论(5) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-03-26 21:40:58

http://blog.chinaunix.net/u1/40527/showart.php?id=473179

chinaunix网友2009-02-02 14:23:52

白老师,怎么才能把一个xml 格式的文件,转成abap 能识别的格式呢,就是得到里面的值!

qdbarry2008-05-08 21:04:30

在这个基础上稍微改一下就可以了: http://blog.chinaunix.net/u1/40527/showart_681421.html

chinaunix网友2008-05-07 15:51:33

老白大哥,请教你关于xml的问题,我想写一个这样的XML: ?xml version="1.0" encoding="utf-8"?> 4800031682 20080320 1000 总公司 110006427 广告公司 10 500 2925.00 2100001073

chinaunix网友2008-01-21 04:27:26

太牛了!我正好在查关于XML应用的一些内容