Posted: Sun Oct 14, 2007 9:48 pm Post subject: XML to ABAP in 4.6C using iXML Library, without XSLT
XML to ABAP in 4.6C ( Inbound ) using iXML Library, without XSLT.
Requirement:
Processing XML to ABAP in 4.6C ( Inbound ) using iXML Library, without XSLT.
Processing:
This program is an example for processing XML files into SAP ( Inbound Interface ) without using XSLT approach and hecnce it can be used even on SAP 4.6C. It uses iXML library to process XML document using DOM parsing. A sample input XML file input_xml.xml is taken as example and kept in C directory.
Code:
*---------------------------------------------------------------------*
* Written By :Ram Manohar Tiwari
* Presented By :www.rmtiwari.com
* Functionality: This program is an example for processing XML files
* into SAP ( Inbound Interface ) without using XSLT
* approach and hence it can be used even on SAP 4.6C.
* It uses iXML library to process XML document using DOM
* parsing. A sample input XML file input_xml.xml is taken
* as example and kept in C directory.
*---------------------------------------------------------------------*
REPORT Z_RMTIWARI_XML_TO_ABAP_46C .
* Load iXML Lib.
type-pools: ixml.
class cl_ixml definition load.
data: G_IXML type ref to if_ixml.
data: STREAMFACTORY type ref to if_ixml_stream_factory.
data: ISTREAM type ref to if_ixml_istream.
data: DOCUMENT type ref to if_ixml_document.
data: PARSER type ref to if_ixml_parser.
data: LV_FILE_URL type rlgrap-filename.
* You should provide the parameter for file name
LV_FILE_URL = 'C:\input_xml.xml'.
types: begin of XML_LINE,
DATA(256) type x,
end of XML_LINE.
types: begin of TY_HEADER,
CUST_NAME(20) type c,
CARD_NO(20) type c,
TAX_AMOUNT(10) type c,
TOTAL_AMOUNT(10) type c,
end of TY_HEADER.
types: begin of TY_ITEM,
ITEM_NO(4) type n,
ITEM_ID(20) type c,
ITEM_TITLE(50) type c,
ITEM_QTY(10) type c,
ITEM_UPRICE(10) type c,
end of TY_ITEM.
data: GV_HEADER type TY_HEADER.
data: GT_ITEM type standard table of TY_ITEM with header line.
data: XML_TABLE type table of XML_LINE,
XML_TABLE_SIZE type i.
* The next step is creating the main factory for the iXML library:
G_IXML = cl_ixml=>create( ).
* Now Create Stream Factory
STREAMFACTORY = G_IXML->create_stream_factory( ).
* upload a file from the client's workstation
call function 'WS_UPLOAD'
exporting
filename = LV_FILE_URL
filetype = 'BIN'
importing
filelength = XML_TABLE_SIZE
tables
data_tab = XML_TABLE
exceptions
others = 11.
* wrap the table containing the file into a stream
ISTREAM = STREAMFACTORY->create_istream_itable( table = XML_TABLE
size = XML_TABLE_SIZE )
.
* Get the file data as Stream
*istream = streamfactory->create_istream_uri( public_id = lv_file_url
* system_id = lv_file_url ).
* Create XML Document instance
DOCUMENT = G_IXML->create_document( ).
* Parse an XML document into a DOM tree
*parser->parse( ).
* Parsing Error Processing
if PARSER->parse( ) ne 0.
if PARSER->num_errors( ) ne 0.
data: PARSEERROR type ref to if_ixml_parse_error,
STR type STRING,
I type i,
COUNT type I,
INDEX type i.
COUNT = PARSER->num_errors( ).
write: COUNT, ' parse errors have occured:'.
INDEX = 0.
while INDEX < COUNT.
PARSEERROR = PARSER->get_error( INDEX = index ).
I = PARSEERROR->get_line( ).
write: 'line: ', i.
I = PARSEERROR->get_column( ).
write: 'column: ', i.
STR = PARSEERROR->get_reason( ).
write: STR.
INDEX = index + 1.
endwhile.
endif.
endif.
* Close the stream since it пїЅs not needed anymore
call method ISTREAM->close( ).
clear ISTREAM.
* Now try to make it look good
* data : lv_size type sytabix,
* lv_ret_code type sysubrc.
*
* data: lo_xml_document type ref to cl_xml_document.
*
* field-symbols: <fs_xml_data> type any table.
*
* lo_xml_document = document.
*
* call method lo_xml_document->get_as_table
* importing
* table = <fs_xml_data>
* size = lv_size
* retcode = lv_ret_code
.
*
*data: items type ref to if_ixml_node_collection.
*
*items = document->get_elements_by_tag_name( name = 'Item' ).
*
*data: iterator type ref to if_ixml_node_iterator,
* node type ref to if_ixml_node.
*
*iterator = document->create_iterator( ).
*node = iterator->get_next( ).
*
*while not node is initial.
*
** do something with the node
*
* ...
*
* node = iterator->get_next( ).
*
*endwhile.
DATA : GV_NODE type ref to if_ixml_node.
DATA : GV_NODETEXT type STRING.
data: GV_FIRST_TIME.
* Get the next child
X_NODE = x_node->get_first_child( ).
* Recurse
while not X_NODE is initial.
perform GET_DATA tables GT_ITEM
using X_NODE
changing GV_HEADER.
X_NODE = x_node->get_next( ).
endwhile.
endform.
input_xml.xml
Code:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>
- <Order>
- <Customer>
<Name>Bill Buckram</Name>
<Cardnum>234 234 234 234</Cardnum>
</Customer>
- <Manifest>
- <Item>
<ID>209</ID>
<Title>Duke: A Biography of the Java Evangelist</Title>
<Quantity>1</Quantity>
<UnitPrice>$10.75</UnitPrice>
</Item>
- <Item>
<ID>208</ID>
<Title>100% Pure: Making Cross Platform Deployment a Reality</Title>
<Quantity>1</Quantity>
<UnitPrice>$10.75</UnitPrice>
</Item>
- <Item>
<ID>204</ID>
<Title>Making the Transition from C++ to the Java(tm) Language</Title>
<Quantity>1</Quantity>
<UnitPrice>$10.75</UnitPrice>
</Item>
- <Item>
<ID>202</ID>
<Title>Web Servers for Fun and Profit</Title>
<Quantity>1</Quantity>
<UnitPrice>$10.75</UnitPrice>
</Item>
- <Item>
<ID>210</ID>
<Title>I Think Not: Dukes Likeness to the Federation Insignia</Title>
<Quantity>1</Quantity>
<UnitPrice>$10.75</UnitPrice>
</Item>
</Manifest>
- <Receipt>
<Subtotal>$53.75</Subtotal>
<Tax>$4.43</Tax>
<Total>$58.18</Total>
</Receipt>
</Order>
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.