方法一: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() 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 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 = 'gsgs-cgdd' ).
L_RC = L_ELEMENT_FLIGHTS->SET_ATTRIBUTE( NAME = 'DES' VALUE = '广深公司-采购订单').
L_RC = L_ELEMENT_FLIGHTS->SET_ATTRIBUTE( NAME = 'BAPI' VALUE ='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 = l_spfli-connid.
L_RC = L_ELEMENT_FLIGHT->SET_ATTRIBUTE( NAME = 'n' VALUE = '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 = NODE->GET_VALUE( ).
WRITE: SPACES, NODETYPE ,NAME,VALUE .
ENDFORM. "printnodeinfo

20170319 ABAP 生成XML文件的更多相关文章

  1. Android 解析XML文件和生成XML文件

    解析XML文件 public static void initXML(Context context) { //can't create in /data/media/0 because permis ...

  2. Java生成XML文件

    我们在数据库中的数据可以将其提取出来生成XML文件,方便传输.例如数据库中有Admin这张表: 我们写一个java类表示admin数据: package xmlDom.vo; import java. ...

  3. Android 使用xml序列化器生成xml文件

    在<Android 生成xml文件>一文中使用流的形式写入xml格式文件,但是存在一定的问题,那就是在短信内容中不能出现<>之类的括号,本文使用xml序列化器来解决 xml序列 ...

  4. C# 生成xml文件

    本篇文章旨在.net环境下生成xml文件,以控制台应用程序为例进行说明. 1.在vs中新建控制台应用程序CreateXml 2.CreateXmlFile:主要生成xml的函数 public void ...

  5. 视频播放实时记录日志并生成XML文件

    需求描述: 在JWPlayer视频播放过程中,要求实时记录视频观看者播放.暂停的时间,并记录从暂停到下一次播放时所经过的时间.将所有记录保存为XML文件,以方便数据库的后续使用. 实现过程: 尝试1: ...

  6. 使用XML序列化器生成XML文件和利用pull解析XML文件

    首先,指定XML格式,我指定的XML格式如下: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <message&g ...

  7. LINQ to XML 从逗号分隔值 (CSV) 文件生成 XML 文件

    参考:http://msdn.microsoft.com/zh-cn/library/bb387090.aspx 本示例演示如何使用 语言集成查询 (LINQ) 和 LINQ to XML 从逗号分隔 ...

  8. 生成XML文件,通过实体生成XML文件

    实体 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xm ...

  9. PHP中的生成XML文件的4种方法(转)

    <?xml version="1.0" encoding="utf-8"?> <article> <item> <ti ...

随机推荐

  1. consul UI用127可以访问,指定ip无法访问

    ./consul agent -dev    只能127.0.0.1可以访问 ./consul agent -dev  -client 0.0.0.0 -ui  指定ip可以访问

  2. Delphi GDI对象之脱屏位图(Offscreen Bitmaps),也叫内存位图

    http://www.cnblogs.com/pchmonster/archive/2012/07/09/2583613.html 脱屏位图(Offscreen Bitmaps) 脱屏位图,也叫内存位 ...

  3. (41)C#异步编程

    VS2010是经常阻塞UI线程的应用程序之一.例如用vs2010打开一个包含数百个项目的解决方案,可以要等待很长时间(感觉像卡死),自从vs2012情况得到了改善,项目在后台进行了异步加载. 一.同步 ...

  4. [Machine Learning with Python] Data Visualization by Matplotlib Library

    Before you can plot anything, you need to specify which backend Matplotlib should use. The simplest ...

  5. Go -- pprof协程监控

    go中有pprof包来做代码的性能监控,在两个地方有包: net/http/pprof runtime/pprof 其实net/http/pprof中只是使用runtime/pprof包来进行封装了一 ...

  6. linux删除空行操作:awk、grep、tr、sed

    如下:如何删除空行 shen\nshen\\n sen seh sehe she she 真正删除空行,可以使用vim: 通过命令模式删除空行.vim在命令模式下(在vim里输入英文字符:进入命令模式 ...

  7. Linux如何显示文件指定行数的内容;显示第一行、中间几行和最后几行

    1.tail -n +/-数字 文件名 2.head -n 数字 文件名 3.sed -n "开始行,结束行p" 文件名 4.sed -n '1p;20,40p; "显示 ...

  8. 深入理解Java中的HashMap的实现原理

    HashMap继承自抽象类AbstractMap,抽象类AbstractMap实现了Map接口.关系图例如以下所看到的: Java中的Map<key, value>接口同意我们将一个对象作 ...

  9. python matplotlib imshow热图坐标替换/映射

    今天遇到了这样一个问题,使用matplotlib绘制热图数组中横纵坐标自然是图片的像素排列顺序, 但是这样带来的问题就是画出来的x,y轴中坐标点的数据任然是x,y在数组中的下标, 实际中我们可能期望坐 ...

  10. Mondiran创建连接

    曾经使用jdbc创建连接的时候使用的url是这种形式:jdbc:mysql://hostname:port/database?key1=value1&key2=value2,在URL须要以&q ...