SAP 生成XML

前端之家收集整理的这篇文章主要介绍了SAP 生成XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
REPORTZXML_A.

SETEXTENDEDCHECKOFF.

TYPE-POOLS:IXML.

*&---------------------------------------------------------------------*
*&ReportT_DOM_MANIPULATE
*&
*&---------------------------------------------------------------------*
*&showshowtomanipulateadomtree
*&---------------------------------------------------------------------*
***********************************************************
*macros

DEFINEXML.

WRITE:/'XML:',&1COLORCOL_POSITIVEINVERSE.

END-OF-DEFINITION.

*************************************************************
*globaldata
*
DATA:DOCUMENTTYPEREFTOIF_IXML_DOCUMENT,

RETTYPEI.
*************************************************************
*buildingaDOMtreefromscratch
*

START-OF-SELECTION.

*createthetree
PERFORMCREATE_DOMCHANGINGDOCUMENT.

*printthetree
ULINE.WRITE:/'DOCUMENTBEFORMANIPULATION'.

PERFORMPRINT_DOMUSINGDOCUMENT.

*renderthetree
PERFORMMANIP_DOMCHANGINGDOCUMENT.

*printthetree
ULINE.WRITE:/'DOCUMENTAFTERMANIPULATION'.

PERFORMPRINT_DOMUSINGDOCUMENT.

*renderthetree
PERFORMRENDER_DOMCHANGINGDOCUMENT.

*&---------------------------------------------------------------------*
*&Formcreate_dom
*&---------------------------------------------------------------------*
*text
*----------------------------------------------------------------------*
*<--P_DOCUMENTtext
*----------------------------------------------------------------------*

FORMCREATE_DOMCHANGINGDOCUMENTTYPEREFTOIF_IXML_DOCUMENT.

*------------------------------
*documenttocreate
*------------------------------

*<?xmlversion="1.0"?>

*<ordernumber="4711">

*<head>

*<status>confirmed</status>

*<dateformat="mm/dd/yyyy">08/15/1999</date>

*</head>

*<body>

*<itempos="10"units="2"price="17">abap-book</item>

*<itempos="20"units="1"price="10">sapr3-cdrom</item>

*<itempos="30"units="5"price="12">coffee</item>

*</body>

*</order>

*------------------------------

DATA:IXMLTYPEREFTOIF_IXML,

PARENTTYPEREFTOIF_IXML_NODE,

ELEMENTTYPEREFTOIF_IXML_ELEMENT,

NODETYPEREFTOIF_IXML_NODE,

RETTYPEI.

*createtheixmlmainfactory

IXML=CL_IXML=>CREATE().

*createaemptydocument

DOCUMENT=IXML->CREATE_DOCUMENT().

*createrootnode<order>"根节点!!!!!!!!!!

ELEMENT=DOCUMENT->CREATE_ELEMENT(NAME='order').

RET=ELEMENT->SET_ATTRIBUTE(NAME='number'VALUE='4711').

RET=DOCUMENT->APPEND_CHILD(NEW_CHILD=ELEMENT).

*remember<order>node

PARENT=ELEMENT.

*createnode<head>

ELEMENT=DOCUMENT->CREATE_ELEMENT(NAME='head').

RET=PARENT->APPEND_CHILD(NEW_CHILD=ELEMENT).

*createnode<body>

ELEMENT=DOCUMENT->CREATE_ELEMENT(NAME='body').

RET=PARENT->APPEND_CHILD(NEW_CHILD=ELEMENT).

*find<head>node

PARENT=DOCUMENT->FIND_FROM_NAME(NAME='head').

*create<status>confirmed</status>

ELEMENT=DOCUMENT->CREATE_ELEMENT(NAME='status').

RET=ELEMENT->SET_VALUE(VALUE='confirmed').

RET=PARENT->APPEND_CHILD(NEW_CHILD=ELEMENT).

*create<dateformat="mm/dd/yyyy">08/15/2001</date>

ELEMENT=DOCUMENT->CREATE_ELEMENT(NAME='date').

RET=ELEMENT->SET_ATTRIBUTE(NAME='format'VALUE='mm/dd/yyyy').

RET=ELEMENT->SET_VALUE(VALUE='08/15/2001').

RET=PARENT->APPEND_CHILD(NEW_CHILD=ELEMENT).

*find<body>node

PARENT=DOCUMENT->FIND_FROM_NAME(NAME='body').

*create<itempos="10"units="2"price="17">abap-book</item>

ELEMENT=DOCUMENT->CREATE_ELEMENT(NAME='item').

RET=ELEMENT->SET_ATTRIBUTE(NAME='pos'VALUE='10').

RET=ELEMENT->SET_ATTRIBUTE(NAME='units'VALUE='2').

RET=ELEMENT->SET_ATTRIBUTE(NAME='price'VALUE='17').

RET=ELEMENT->SET_ATTRIBUTE(NAME='st'VALUE='1').

RET=ELEMENT->SET_VALUE(VALUE='abap-book').

RET=PARENT->APPEND_CHILD(NEW_CHILD=ELEMENT).

*create<itempos="20"units="1"price="10">sapr3-cdrom</item>

ELEMENT=DOCUMENT->CREATE_ELEMENT(NAME='item').

RET=ELEMENT->SET_ATTRIBUTE(NAME='pos'VALUE='20').

RET=ELEMENT->SET_ATTRIBUTE(NAME='units'VALUE='1').

RET=ELEMENT->SET_ATTRIBUTE(NAME='price'VALUE='10').

RET=ELEMENT->SET_ATTRIBUTE(NAME='st'VALUE='2').

RET=ELEMENT->SET_VALUE(VALUE='sapr3-cdrom').

RET=PARENT->APPEND_CHILD(NEW_CHILD=ELEMENT).

*create<itempos="30"units="5"price="12">coffee</item>

ELEMENT=DOCUMENT->CREATE_ELEMENT(NAME='item').

RET=ELEMENT->SET_ATTRIBUTE(NAME='pos'VALUE='30').

RET=ELEMENT->SET_ATTRIBUTE(NAME='units'VALUE='5').

RET=ELEMENT->SET_ATTRIBUTE(NAME='price'VALUE='10').

RET=ELEMENT->SET_ATTRIBUTE(NAME='st'VALUE='3').

RET=ELEMENT->SET_VALUE(VALUE='coffee').

RET=PARENT->APPEND_CHILD(NEW_CHILD=ELEMENT).

ENDFORM."create_dom

*&---------------------------------------------------------------------*
*&Formrender_dom
*&---------------------------------------------------------------------*
*text
*----------------------------------------------------------------------*
*<--P_DOCUMENTtext
*----------------------------------------------------------------------*

FORMRENDER_DOMCHANGINGP_DOCUMENT.

*

*renderDOMintoastring

*

TYPES:C_LINE(50)TYPEC.

DATA:IXMLTYPEREFTOIF_IXML,

RENDERERTYPEREFTOIF_IXML_RENDERER,

STREAMFACTORYTYPEREFTOIF_IXML_STREAM_FACTORY,

OSTREAMTYPEREFTOIF_IXML_OSTREAM,

ENCODINGTYPEREFTOIF_IXML_ENCODING,

XMLLINETYPEC_LINE,

XMLTABTYPESTANDARDTABLEOFC_LINE,

CHARSETTYPESTRING,

SUCCESSTYPEC.

*createtheixmlmainfactory

IXML=CL_IXML=>CREATE().

*createastreamFactory

STREAMFACTORY=IXML->CREATE_STREAM_FACTORY().

*createaoutputstream

OSTREAM=STREAMFACTORY->CREATE_OSTREAM_ITABLE(TABLE=XMLTAB).

*setandocumentencoding(optional)

"charsets:utf-8,utf-16,sap*

IFNOTCHARSETISINITIAL.

ENCODING=IXML->CREATE_ENCODING(CHARACTER_SET=CHARSET

BYTE_ORDER=0).

SUCCESS=OSTREAM->SET_ENCODING(ENCODING=ENCODING).

ENDIF.

*createaxmlrenderer

RENDERER=IXML->CREATE_RENDERER(DOCUMENT=DOCUMENT

OSTREAM=OSTREAM).

*renderdocumentintooutput-stream

IFRENDERER->RENDER()<>0.

*error

WRITE:/'ERROR:documentrendering'.

EXIT.

ENDIF.

*dumpxmltable

ULINE.

WRITE:/.

WRITE:/'RENDEREDXML-DATA'.

WRITE:/.

LOOPATXMLTABINTOXMLLINE.

XMLXMLLINE.

ENDLOOP.

ENDFORM."render_dom

*---------------------------------------------------------------------*
*ORMprint_tree*
*---------------------------------------------------------------------*
*........*
*---------------------------------------------------------------------*

FORMPRINT_DOMUSINGDOCUMENTTYPEREFTOIF_IXML_DOCUMENT.

DATA:NODETYPEREFTOIF_IXML_NODE,

ITERATORTYPEREFTOIF_IXML_NODE_ITERATOR,

NODEMAPTYPEREFTOIF_IXML_NAMED_NODE_MAP,

ATTRTYPEREFTOIF_IXML_NODE,

NAMETYPESTRING,

PREFIXTYPESTRING,

VALUETYPESTRING,

INDENTTYPEI,

COUNTTYPEI,

INDEXTYPEI.

CHECKNOTDOCUMENTISINITIAL.

NODE=DOCUMENT.

ULINE.

WRITE:/.

WRITE:/'DOM-TREE'.

WRITE:/.

IFNODEISINITIAL.EXIT.ENDIF.

*createanodeiterator

ITERATOR=NODE->CREATE_ITERATOR().

*getcurrentnode

NODE=ITERATOR->GET_NEXT().

*loopoverallnodes

WHILENOTNODEISINITIAL.

INDENT=NODE->GET_HEIGHT()*2.

INDENT=INDENT+20.

CASENODE->GET_TYPE().

WHENIF_IXML_NODE=>CO_NODE_ELEMENT.

*elementnode

NAME=NODE->GET_NAME().

NODEMAP=NODE->GET_ATTRIBUTES().

WRITE:/'ELEMENT:'.

WRITE:ATINDENTNAMECOLORCOL_POSITIVEINVERSE.

IFNOTNODEMAPISINITIAL.

*attributes

COUNT=NODEMAP->GET_LENGTH().

DOCOUNTTIMES.

INDEX=SY-INDEX-1.

ATTR=NODEMAP->GET_ITEM(INDEX).

NAME=ATTR->GET_NAME().

PREFIX=ATTR->GET_NAMESPACE().

VALUE=ATTR->GET_VALUE().

WRITE:/'ATTRIBUTE:'.

WRITE:ATINDENTNAMECOLORCOL_HEADINGINVERSE,'=',

VALUECOLORCOL_TOTALINVERSE.

ENDDO.

ENDIF.

WHENIF_IXML_NODE=>CO_NODE_TEXTOR

IF_IXML_NODE=>CO_NODE_CDATA_SECTION.

*textnode

VALUE=NODE->GET_VALUE().

WRITE:/'TEXT:'.

WRITE:ATINDENTVALUECOLORCOL_GROUPINVERSE.

ENDCASE.

*advancetonextnode

NODE=ITERATOR->GET_NEXT().

ENDWHILE.

ENDFORM."traverse_dom

*&---------------------------------------------------------------------*
*&Formdisplay_dom
*&---------------------------------------------------------------------*
*text
*----------------------------------------------------------------------*
*-->P_DOCUMENTtext
*----------------------------------------------------------------------*

FORMDISPLAY_DOMUSINGDOCUMENTTYPEREFTOIF_IXML_DOCUMENT

TITELLIKESY-TITLE.

*displaydocument

CALLFUNCTION'SDIXML_DOM_TO_SCREEN'
EXPORTING
DOCUMENT=DOCUMENT
TITLE=TITEL.

ENDFORM."display_dom

*&---------------------------------------------------------------------*
*&Formmanip_dom
*&---------------------------------------------------------------------*
*text
*----------------------------------------------------------------------*
*<--P_DOCUMENTtext
*----------------------------------------------------------------------*

FORMMANIP_DOMCHANGINGDOCUMENTTYPEREFTOIF_IXML_DOCUMENT.

*------------------------------

*documentnowlookslike

*------------------------------

*<?xmlversion="1.0"?>

*<ordernumber="4711">

*<head>

*<status>confirmed</status>

*<dateformat="mm/dd/yyyy">08/15/1999</date>

*</head>

*<body>

*<itempos="10"units="2"price="17">abap-book</item>

*<itempos="20"units="1"price="10">sapr3-cdrom</item>

*<itempos="30"units="5"price="12">coffee</item>

*</body>

*</order>

*------------------------------

DATA:ORDERTYPEREFTOIF_IXML_ELEMENT,

HEADTYPEREFTOIF_IXML_ELEMENT,

BODYTYPEREFTOIF_IXML_ELEMENT,

STATUSTYPEREFTOIF_IXML_ELEMENT,

DATETYPEREFTOIF_IXML_ELEMENT.

*usingthemethodfind_from_name

ORDER=DOCUMENT->FIND_FROM_NAME(NAME='order').

HEAD=DOCUMENT->FIND_FROM_NAME(NAME='head').

BODY=DOCUMENT->FIND_FROM_NAME(NAME='body').

STATUS=DOCUMENT->FIND_FROM_NAME(NAME='status').

DATE=DOCUMENT->FIND_FROM_NAME(NAME='date').

*update<order>

DATA:OLDTYPESTRING.

OLD=ORDER->GET_ATTRIBUTE(NAME='number')."old=4711

RET=ORDER->SET_ATTRIBUTE(NAME='赵博良'VALUE='001')."ret=0

*update<date>

RET=DATE->SET_ATTRIBUTE(NAME='format'VALUE='mm-dd-yyyy').

RET=DATE->SET_VALUE(VALUE='12-01-2000').

*update<item>

DATA:ITEMSTYPEREFTOIF_IXML_NODE_COLLECTION,

ITEMTYPEREFTOIF_IXML_ELEMENT,

VALTYPESTRING,

IDXTYPEI,

LENTYPEI.

ITEMS=BODY->GET_ELEMENTS_BY_TAG_NAME(NAME='item').

LEN=ITEMS->GET_LENGTH().

DOLENTIMES.

*getitem

IDX=SY-INDEX-1.

IFSY-SAPRL>='610'."gui的版本???????

*REL>=6.10:weusesimplecast

ITEM?=ITEMS->GET_ITEM(INDEX=IDX).

ELSE.

*REL<6.10:explicitquery_interfaceisrequired

NODE=ITEMS->GET_ITEM(INDEX=IDX).

ITEM?=NODE->QUERY_INTERFACE(IID=IXML_IID_ELEMENT).

ENDIF.

*changepos

VAL=SY-INDEX.

RET=ITEM->SET_ATTRIBUTE(NAME='pos'VALUE=VAL).

*removeattrst='X'

RET=ITEM->REMOVE_ATTRIBUTE(NAME='st')."删除了st节点

*addattrln='X'

RET=ITEM->SET_ATTRIBUTE(NAME='idx'VALUE=VAL).

ENDDO.

*------------------------------

*documentnowlookslike

*------------------------------

*<?xmlversion="1.0"?>

*<ordernumber="4711"type="001">

*<head>

*<status>confirmed</status>

*<dateformat="mm-dd-yyyy">12-01-2000</date>

*</head>

*<body>

*<itempos="1"units="2"price="17"idx="1">abap-book</item>

*<itempos="2"units="1"price="10"idx="2">sapr3-cdrom</item>

*<itempos="3"units="5"price="10"idx="3">coffee</item>

*</body>

*</order>

*------------------------------

ENDFORM."manip_dom



http://blog.csdn.net/wren2004/archive/2010/03/11/5369325.aspx

http://zbl.0201.blog.163.com/blog/static/66203766200862013550673/

猜你在找的XML相关文章