首页 > 代码库 > 20170319 ABAP 生成XML文件
20170319 ABAP 生成XML文件
方法一:ABAP 使用method方式操作XML
转自:http://www.cnblogs.com/jiangzhengjun/p/4265595.html
方法二:STRANS 转换工具;使用strans 开发将内表转换xml文件,并可以将xml文件解析回到内表;
参考:http://www.cnblogs.com/wuqingbo/p/6229289.html
->一、XML定义?
if_ixml
if_ixml_document
if_ixml_node
if_ixml_element
if_ixml_istream
if_ixml_ostream
document、element、ATTRIBUTE、COMMENT、TEXT 都属于Node
生成: <?xml version="1.0"?> -<flow BAPI="ZBAPI_MM_RK_AFTER_APP"Des="广深公司-采购订单"KEY="gsgs-cgdd"> -<customform> -<fd n="flight"> <V>110000</V> -</fd> -<fd n="flight"> <V>090000</V> -</fd> </customform> -</flow>
*&---------------------------------------------------------------------* *& Report YTST_201703_XML001 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT YTST_201703_XML001. TYPE-POOLS: IXML,ABAP. TYPES: BEGIN OF XML_LINE, * DATA(512) TYPE X,"这里的长度设置不会影响输出结果,设置成1都可以 DATA TYPE X,"这里的长度设置不会影响输出结果,设置成1都可以 END OF XML_LINE. DATA: L_IXML TYPE REF TO IF_IXML, L_STREAMFACTORY TYPE REF TO IF_IXML_STREAM_FACTORY, L_OSTREAM TYPE REF TO IF_IXML_OSTREAM, L_RENDERER TYPE REF TO IF_IXML_RENDERER, L_DOCUMENT TYPE REF TO IF_IXML_DOCUMENT. DATA: L_ELEMENT_FLIGHTS TYPE REF TO IF_IXML_ELEMENT, L_ELEMENT_AIRLINE TYPE REF TO IF_IXML_ELEMENT, L_ELEMENT_FLIGHT TYPE REF TO IF_IXML_ELEMENT, L_ELEMENT_DUMMY TYPE REF TO IF_IXML_ELEMENT, L_VALUE TYPE STRING. DATA: L_XML_TABLE TYPE TABLE OF XML_LINE WITH HEADER LINE, L_XML_SIZE TYPE I, L_RC TYPE I. DATA: LT_SPFLI TYPE TABLE OF SPFLI. DATA: L_SPFLI TYPE SPFLI. START-OF-SELECTION. SELECT * FROM SPFLI INTO TABLE LT_SPFLI UP TO 2 ROWS. SORT LT_SPFLI BY CARRID. * 生成XML数据 LOOP AT LT_SPFLI INTO L_SPFLI. AT FIRST. * Creating a ixml factory L_IXML = CL_IXML=>CREATE( ). * Creating the dom object model L_DOCUMENT = L_IXML->CREATE_DOCUMENT( ). * Fill root node with value flow L_ELEMENT_FLIGHTS = L_DOCUMENT->CREATE_SIMPLE_ELEMENT( NAME = ‘flow‘ PARENT = L_DOCUMENT ). L_RC = L_ELEMENT_FLIGHTS->SET_ATTRIBUTE( NAME = ‘KEY‘ VALUE = http://www.mamicode.com/‘gsgs-cgdd‘ ). L_RC = L_ELEMENT_FLIGHTS->SET_ATTRIBUTE( NAME = ‘DES‘ VALUE = http://www.mamicode.com/‘广深公司-采购订单‘). L_RC = L_ELEMENT_FLIGHTS->SET_ATTRIBUTE( NAME = ‘BAPI‘ VALUE =http://www.mamicode.com/‘ZBAPI_MM_RK_AFTER_APP‘ ). L_ELEMENT_AIRLINE = L_DOCUMENT->CREATE_SIMPLE_ELEMENT( NAME = ‘customform‘ PARENT = L_ELEMENT_FLIGHTS ). "parent为父节点 ENDAT. AT NEW CONNID. L_ELEMENT_FLIGHT = L_DOCUMENT->CREATE_SIMPLE_ELEMENT( NAME = ‘fd‘ PARENT = L_ELEMENT_AIRLINE ). "l_value = http://www.mamicode.com/l_spfli-connid. L_RC = L_ELEMENT_FLIGHT->SET_ATTRIBUTE( NAME = ‘n‘ VALUE = http://www.mamicode.com/‘flight‘ ). ENDAT. L_VALUE = L_SPFLI-DEPTIME. L_ELEMENT_DUMMY = L_DOCUMENT->CREATE_SIMPLE_ELEMENT( NAME = ‘V‘ VALUE = L_VALUE PARENT = L_ELEMENT_FLIGHT ). ENDLOOP. * Creating a stream factory L_STREAMFACTORY = L_IXML->CREATE_STREAM_FACTORY( )."[STRI:M] 流 * Connect internal XML table to stream factory L_OSTREAM = L_STREAMFACTORY->CREATE_OSTREAM_ITABLE( TABLE = L_XML_TABLE[] ). * Rendering the document L_RENDERER = L_IXML->CREATE_RENDERER( OSTREAM = L_OSTREAM "[?REND?] IMAGE396 DOCUMENT = L_DOCUMENT )."l_document为根节点 L_RC = L_RENDERER->RENDER( ). "注:执行此句后, l_xml_table内表里才会有数据 L_XML_SIZE = L_OSTREAM->GET_NUM_WRITTEN_RAW( )."取得XML数据大小
2.解析
ABAP程序解析XML
*&s1.读取xml文件,
*&s2.
(1)create
(2)create document
(3)create STREAM FACTORY
(4)create ISTREAM ITABLE
IXML = CL_IXML=>CREATE( ).
DOCUMENT = IXML->CREATE_DOCUMENT( ).
STREAMFACTORY = IXML->CREATE_STREAM_FACTORY( ).
ISTREAM = STREAMFACTORY->CREATE_ISTREAM_ITABLE( TABLE = XML_TABLE
SIZE = TOTALSIZE ).
*a program for parsing.
*A computer equipped with an XML parser can make decisions about this
*information.
PARSER = IXML->CREATE_PARSER( STREAM_FACTORY = STREAMFACTORY
ISTREAM = ISTREAM
DOCUMENT = DOCUMENT ).
*&s3.分节点进行解析:
CALL METHOD ISTREAM->CLOSE( ).
CLEAR ISTREAM.
NODE = DOCUMENT.
PERFORM PRINT_NODE USING NODE 0.
*&---------------------------------------------------------------------* *& Report YTST_201703_XML002 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT YTST_201703_XML002. TYPE-POOLS: IXML. DATA: IXML TYPE REF TO IF_IXML, DOCUMENT TYPE REF TO IF_IXML_DOCUMENT, STREAMFACTORY TYPE REF TO IF_IXML_STREAM_FACTORY, ISTREAM TYPE REF TO IF_IXML_ISTREAM, PARSER TYPE REF TO IF_IXML_PARSER, NODE TYPE REF TO IF_IXML_NODE, STRING TYPE STRING, COUNT TYPE I, INDEX TYPE I, TOTALSIZE TYPE I . TYPES: BEGIN OF XML_LINE, DATA(256) TYPE X, END OF XML_LINE. DATA: XML_TABLE TYPE TABLE OF XML_LINE. START-OF-SELECTION. *&s1.上传文件 CALL FUNCTION ‘GUI_UPLOAD‘ EXPORTING FILENAME = ‘d:\flights.xml‘ FILETYPE = ‘BIN‘ IMPORTING FILELENGTH = TOTALSIZE TABLES DATA_TAB = XML_TABLE EXCEPTIONS OTHERS = 11. IF SY-SUBRC <> 0. EXIT. ENDIF. *&s2. *(1)create *(2)create document *(3)create STREAM FACTORY *(4)create ISTREAM ITABLE *(5)create PARSER IXML = CL_IXML=>CREATE( ). DOCUMENT = IXML->CREATE_DOCUMENT( ). STREAMFACTORY = IXML->CREATE_STREAM_FACTORY( ). ISTREAM = STREAMFACTORY->CREATE_ISTREAM_ITABLE( TABLE = XML_TABLE SIZE = TOTALSIZE ). PARSER = IXML->CREATE_PARSER( STREAM_FACTORY = STREAMFACTORY ISTREAM = ISTREAM DOCUMENT = DOCUMENT ). IF PARSER->PARSE( ) NE 0. IF PARSER->NUM_ERRORS( ) NE 0. COUNT = PARSER->NUM_ERRORS( ). WRITE: COUNT, ‘ parse errors have occured:‘. DATA: PPARSEERROR TYPE REF TO IF_IXML_PARSE_ERROR, I TYPE I. INDEX = 0. WHILE INDEX < COUNT. PPARSEERROR = PARSER->GET_ERROR( INDEX = INDEX ). I = PPARSEERROR->GET_LINE( ). WRITE: ‘line: ‘, I. I = PPARSEERROR->GET_COLUMN( ). WRITE: ‘column: ‘, I. STRING = PPARSEERROR->GET_REASON( ). WRITE: STRING. INDEX = INDEX + 1. ENDWHILE. ENDIF. ENDIF. CALL METHOD ISTREAM->CLOSE( ). CLEAR ISTREAM. NODE = DOCUMENT. PERFORM PRINT_NODE USING NODE 0. *&---------------------------------------------------------------------* *& Form print_node *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_NODE text * -->DEEP text *----------------------------------------------------------------------* FORM PRINT_NODE USING P_NODE TYPE REF TO IF_IXML_NODE DEEP TYPE I. DATA: NODETYPE TYPE I, ATTRSLEN TYPE I, ATTRS TYPE REF TO IF_IXML_NAMED_NODE_MAP, ATTR TYPE REF TO IF_IXML_NODE. NODETYPE = P_NODE->GET_TYPE( ). CASE P_NODE->GET_TYPE( ). WHEN IF_IXML_NODE=>CO_NODE_ELEMENT."这里只处理元素节点 WRITE: /. PERFORM PRINTNODEINFO USING ‘元素‘ DEEP P_NODE. ATTRS = P_NODE->GET_ATTRIBUTES( ). ATTRSLEN = ATTRS->GET_LENGTH( ). DO ATTRSLEN TIMES. ATTR = ATTRS->GET_ITEM( SY-INDEX - 1 ). PERFORM PRINTNODEINFO USING ‘属性‘ DEEP ATTR. ENDDO. "WHEN if_ixml_node=>co_node_text. "PERFORM printnodeinfo USING ‘文本‘ deep p_node. ENDCASE. DATA: CHILDS TYPE REF TO IF_IXML_NODE_LIST, CHILD TYPE REF TO IF_IXML_NODE, CHILDSLEN TYPE I. CHILDS = P_NODE->GET_CHILDREN( ). CHILDSLEN = CHILDS->GET_LENGTH( ). DATA: DEEP2 TYPE I. DEEP2 = DEEP + 1. DO CHILDSLEN TIMES. CHILD = CHILDS->GET_ITEM( SY-INDEX - 1 ). PERFORM PRINT_NODE USING CHILD DEEP2. ENDDO. ENDFORM. "print_node *&---------------------------------------------------------------------* *& Form printnodeinfo *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->NODETYPE text * -->DEEP text * -->NODE text *----------------------------------------------------------------------* FORM PRINTNODEINFO USING NODETYPE TYPE STRING DEEP TYPE I NODE TYPE REF TO IF_IXML_NODE. DATA: NAME TYPE STRING, VALUE TYPE STRING, SPACES TYPE STRING. DO DEEP TIMES. SPACES = SPACES && ` `. ENDDO. NAME = NODE->GET_NAME( ). VALUE = http://www.mamicode.com/NODE->GET_VALUE( )."printnodeinfo
20170319 ABAP 生成XML文件