10-07-2008 4:55 PM
Hi,
Can anyone tell me how to convert XML to INTERNAL table.
HERE is the XML which i need to convert into an ABAP table.
I am confused even about the way the structure i have to use.
<?xml version="1.0" encoding="UTF-8" ?>
- <Status>
<ErrCode>0</ErrCode>
<ErrDesc>OK</ErrDesc>
</Status>
<Cost>10000</Cost>
<Currency>GBP</Currency>
- <Voyage v="1">
<DEPT>ARREY</DEPT>
- <Passengers>
<NumAdults>2</NumAdults>
<NumSeniors>1</NumSeniors>
<NumChildrenC3>1</NumChildrenC3>
- <Passenger p="1">
<PList>1</PList>
<Type>A</Type>
<Title>Mr</Title>
<Forename>RAJ</Forename>
<Surname>FRN</Surname>
<Age>30</Age>
<Gender>M</Gender>
<Nationality>INR</Nationality>
- <PassportDetails>
<Number>2343</Number>
<IssuePlace>SDFG</IssuePlace>
<IssueDate>34544</IssueDate>
<ExpiryDate>345344</ExpiryDate>
</PassportDetails>
<DateOfBirth>19740401</DateOfBirth>
<Language>EN</Language>
- <Address>
<AddressLine a="1">2 The Avenue</AddressLine>
<AddressLine a="2">Oxford</AddressLine>
<AddressLine a="3" />
<AddressLine a="4" />
<Postcode>OX1 6AB</Postcode>
</Address>
- <Phones>
<Home>34534</Home>
<Fax>345345</Fax>
<Mobile>3453453</Mobile>
</Phones>
</Passenger>
</Passengers>
- <Vehicles>
- <Vehicle v="1">
<Type>CAR</Type>
<MakeModel>Ford Cortina</MakeModel>
<RegNum>345345</RegNum>
<Length>450</Length>
<Height>150</Height>
<Width>200</Width>
<RoofWidth>190</RoofWidth>
<Weight>2000</Weight>
<NumAxles>2</NumAxles>
- <VehPricing>
<Measure>feet</Measure>
<Number>30</Number>
</VehPricing>
<Load>Animals</Load>
- <Trailer>
<Type>CRV</Type>
<MakeModel>Ifor Williams</MakeModel>
<Length>400</Length>
<Height>180</Height>
<Width>200</Width>
<RoofWidth>200</RoofWidth>
<Weight>2000</Weight>
<NumAxles>1</NumAxles>
- <TrlPricing>
<Measure>tons</Measure>
<Number>10</Number>
</TrlPricing>
<Load>Animals</Load>
</Trailer>
</Vehicle>
</Vehicles>
- <Accommodations>
<Code>A4</Code>
<NumItems>1</NumItems>
<Desc>4 berth outside cabin</Desc>
</Accommodation>
- <AccAllocations>
<AccAllocation Pax="1" Code="A4" Share="1" Location="201" Cabin="3" Berth="1" />
<AccAllocation Pax="2" Code="A4" Share="1" Location="201" Cabin="3" Berth="2" />
<AccAllocation Pax="3" Code="A4" Share="1" Location="201" Cabin="3" Berth="3" />
</AccAllocations>
</Accommodations>
</Voyage>
Regards,
Rajender.
10-07-2008 8:48 PM
This example will parse that file and put it into an internal table:
report ZXMLIMPORT message-id bcciixmlt3_msg line-size 1000.
*DATA DECLARATION
type-pools: ixml.
data:begin of tmp_lines_memo occurs 0 ,
itm like BAPIITEMIN-ITM_NUMBER,
TDLINE(132) type c,
end of tmp_lines_memo.
data:begin of xml_file_list occurs 0,
name(250) type c,
value type string,
end of xml_file_list.
parameter:file(255) type c OBLIGATORY.
start-of-selection.
*-- data
data: piXML type ref to if_ixml,
pDocument type ref to if_ixml_document,
pStreamFactory type ref to if_ixml_stream_factory,
pIStream type ref to if_ixml_istream,
pParser type ref to if_ixml_parser,
pNode type ref to if_ixml_node,
pText type ref to if_ixml_text,
string type string,
count type i,
index type i,
totalSize type i,
dsn(40) type C,
xstr type xstring.
*-- read the XML document from the frontend machine
*___
types: begin of xml_line,
data(256) type X,
* data(256) type C,
end of xml_line.
data: xml_table type table of xml_line.
*DATA: XML_TABLE TYPE DCXMLLINES,
*XML_LINE LIKE LINE OF XML_TABLE.
*___
data:len TYPE i.
data:xml_file like line of xml_table.
data: filename type string.
filename = file.
if filename = space.
MESSAGE 'Invalid file name' TYPE 'I'.
EXIT.
endif.
CONCATENATE 'Processing ' filename into string.
MESSAGE STRING TYPE 'I'.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = filename
FILETYPE = 'BIN' "#EC NOTEXT
IMPORTING
FILELENGTH = totalSize
TABLES
DATA_TAB = xml_table
EXCEPTIONS
OTHERS = 11.
if sy-subrc <> 0.
message w000.
exit.
endif.
*-- create the main factory
pixml = cl_ixml=>create( ).
*-- create the initial document
pDocument = pixml->create_document( ).
*-- create the stream factory
pStreamFactory = pixml->create_stream_factory( ).
*-- create an input stream for the table
pIStream = pStreamFactory->create_istream_itable( table = xml_table
size = totalSize ).
*-- create the parser
pParser = piXML->create_parser( stream_factory = pStreamFactory
istream = pIStream
document = pDocument ).
*-- parse the stream
if pParser->parse( ) ne 0.
if pParser->num_errors( ) ne 0.
count = pParser->num_errors( ).
write: count, ' parse errors have occured:'. "#EC NOTEXT
data: pParseError type ref to if_ixml_parse_error,
i type i.
index = 0.
while index < count.
pParseError = pParser->get_error( index = index ).
i = pParseError->get_line( ).
* write: 'line: ', i. "#EC NOTEXT
i = pParseError->get_column( ).
* write: 'column: ', i. "#EC NOTEXT
string = pParseError->get_reason( ).
write: string.
MESSAGE STRING TYPE 'I'.
index = index + 1.
endwhile.
endif.
endif.
*-- we don't need the stream any more, so let's close it...
call method pIStream->close( ).
clear pIStream.
*-- if Count > 0 then the XML File had troubles like incomplete tags, missing parts, etc.
IF COUNT > 0.
zreturn = ' parse errors have occured:'.
perform error_file_move.
EXIT.
ENDIF.
*-- print the whole DOM tree as a list...
pNode = pDocument.
clear i_field.
perform print_node using pNode.
*---------------------------------------------------------------------*
* FORM print_node *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
form print_node using value(pNode) type ref to if_ixml_node.
data: indent type i.
data: pText type ref to if_ixml_text.
data: string type string.
data: name type string.
data: value type string.
indent = pNode->get_height( ) * 2.
case pnode->get_type( ).
when if_ixml_node=>co_node_element.
name = pNode->get_name( ).
* write:/ name, '=>'.
*Insert all fields from XML into an internal table
append xml_file_list.
CLEAR xml_file_list-value.
*Here comes the field name from the XML File
xml_file_list-name = name.
TRANSLATE xml_file_list-name TO UPPER CASE.
when if_ixml_node=>co_node_text.
pText ?= pNode->query_interface( ixml_iid_text ).
if pText->ws_only( ) is initial.
*Here comes the field value
value = pNode->get_value( ).
xml_file_list-value = value.
* write: value.
endif.
endcase.
pNode = pNode->get_first_child( ).
while not pNode is initial.
perform print_node using pNode.
pNode = pNode->get_next( ).
endwhile.
endform. "print_node
10-07-2008 9:02 PM
Hi,
1. read the xml into a string
2. convert the string to xstring using SCMS_STRING_TO_XSTRING
3. now use FM SMUM_XML_PARSE and pass the xstring xml_input parameter of that function.
this will parse the whole xml and give you an itab of element name value pairs which you can read easily
Uwe has nice code there ...
Regards,
Karol
Edited by: Karol Seman on Oct 7, 2008 10:06 PM
10-07-2008 11:29 PM
Rajendra,
For this you need to write XSLT program to convert from XML to internal table.
Here are some tips to do so.
1. Write XSLT program. T.code XSLT . e.g. XSLT name "ZTRANS".
2. Write ABAP program
Which includes declaration of internal tables
that you need "IT_DATA".
Upload XML data to an internal table "IT_XML "
use below statement to convert XML to internal table.
Call transformation ZTRANS
source XML IT_XML
result IT_DATA.
Follow below link for SAP example for the same.
[http://help.sap.com/saphelp_nw70/helpdata/en/a4/1f1e400191f72ee10000000a1550b0/content.htm]
I worked on serialization of data side(internal table data to XML ).
Thanks,
Amara.
06-08-2009 1:10 PM
hi~I am facing a problem with xslt transaction.
the following are the codes in ABAP program:
WA_SOURCE-NAME = 'D2CXML'.
GET REFERENCE OF T_FINAL_XML INTO WA_SOURCE-VALUE.
APPEND WA_SOURCE TO T_SOURCE.
CLEAR G_XML_STRING.
CALL TRANSFORMATION ZFIGL4
SOURCE (T_SOURCE)
RESULT XML G_XML_STRING.
IF SY-SUBRC <> 0.
*
ELSE.
G_XML_STRING1 = G_XML_STRING.
REPLACE 'utf-16' WITH 'utf-8' INTO G_XML_STRING.
ENDIF.
the following are the codes in XSLT transaction ZFIGL4:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!xsl:output encoding="utf-8" indent="yes"/>
<xsl:template match="D2CXML">
<voucher_id>
<xsl:apply-templates/>
</voucher_id>
</xsl:template>
<xsl:template match="item">
<voucher_head>
<xsl:attribute name="no">
<xsl:value-of select="AC_DOC_NO"/>
</xsl:attribute>
<company>
<xsl:apply-templates select="COMP_CODE"/>
</company>
<voucher_type>
<xsl:apply-templates select="AC_DOC_TYP"/>
</voucher_type>
<fiscal_year>
<xsl:apply-templates select="GJAHR"/>
</fiscal_year>
<accounting_period>
<xsl:apply-templates select="MONAT"/>
</accounting_period>
<voucher_id>
<xsl:apply-templates select="AC_DOC_NO"/>
</voucher_id>
<prepareddate>
<xsl:apply-templates select="PSTNG_DATE"/>
</prepareddate>
<xsl:apply-templates select="X-MLSTRUCT"/>
</voucher_head>
</xsl:template>
<xsl:template match="X-MLSTRUCT">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="item/X-MLSTRUCT">
<entry>
<entry_id>
<xsl:apply-templates select="ITEM_NUM"/>
</entry_id>
<account_code>
<xsl:apply-templates select="GL_ACCOUNT"/>
</account_code>
<abstract>
<xsl:apply-templates select="POSTXT"/>
</abstract>
<primary_debit_amount>
<xsl:apply-templates select="DEBIT_DC"/>
</primary_debit_amount>
<natural_debit_currency>
<xsl:apply-templates select="DEBIT_DC"/>
</natural_debit_currency>
<primary_credit_amount>
<xsl:apply-templates select="CREDIT_LC"/>
</primary_credit_amount>
</entry>
</xsl:template>
</xsl:transform>
But the result shows in XML file as this :
<?xml version="1.0" encoding="utf-8" ?>
<voucher_id />
No data in the internel table is output at all .DId i miss anything ?
Edited by: leen_202 on Jun 8, 2009 2:10 PM
Edited by: leen_202 on Jun 8, 2009 2:11 PM
10-09-2008 2:40 PM
Hi Rajender,
I've written a tutorial that can help you I believe:
(It contains an example with complete source code)
Best wishes,
Jan
Edited by: Jan Krohn on Sep 12, 2011 10:07 AM - updated link
11-05-2013 11:53 AM