11-29-2007 7:35 AM
Hi,
i need to convert an internal table to XML. I found class CL_XML_DOCUMENT, wehre i can put an internal table, display it as xml, save it as xml in a local file on the presentation server, but i miss retrieving of the xml document as a string table, Method get_as_table dumps for an unknown reason. Does some have a code snippet of how to put an internal table into that and getting the result as a string tab? Or may be another class/function group which will offer this?
11-29-2007 7:37 AM
11-29-2007 7:46 AM
Please read my question carefully. I dont have any problems putting that to a file, i want to have the XML as a string table.
11-29-2007 7:53 AM
Hi,
Check whether the following piece of helps u.
LOOP AT i_xml INTO w_xml.
AT FIRST .
v_text = text-001. " Purchase Order Collection
Creating a ixml factory
v_ixml = cl_ixml=>create( ).
Creating the dom object model
v_document = v_ixml->create_document( ).
Fill root node with value PurchaseOrderCollection
v_element_podes = v_document->create_simple_element(
name = v_text
parent = v_document ).
ENDAT.
v_origin = w_xml-country.
v_supplier = w_xml-supplier.
v_mode = w_xml-mode.
v_buyingterms = w_xml-buyingterms.
v_buyingagent = w_xml-buyingagent.
***********************Conversion of Date format************************
v_orddate = w_xml-orderdate+6(2).
v_ordmonth = w_xml-orderdate+4(2).
v_ordyear = w_xml-orderdate+0(4).
CONCATENATE v_orddate v_slash v_ordmonth v_slash v_ordyear INTO v_orderdate.
v_shpdate = w_xml-shipdate+6(2).
v_shpmonth = w_xml-shipdate+4(2).
v_shpyear = w_xml-shipdate+0(4).
CONCATENATE v_shpdate v_slash v_shpmonth v_slash v_shpyear INTO v_shipdate.
v_delvdate = w_xml-deliverydate+6(2).
v_delvmonth = w_xml-deliverydate+4(2).
v_delvyear = w_xml-deliverydate+0(4).
CONCATENATE v_delvdate v_slash v_delvmonth v_slash v_delvyear INTO v_deliverydate.
AT NEW purchaseorder .
Create element 'PurchaseOrder' as child of 'PurchaseOrderCollection'
v_text = text-002. " Purchase Order
v_element_poid = v_document->create_simple_element(
name = v_text
parent = v_element_podes ).
Create attribute 'ID' of node 'PurchaseOrder'
v_value = w_xml-purchaseorder.
v_rc = v_element_poid->set_attribute( name = 'id' value = v_value ).
********* Filling up header fields ******* START
Create element 'Order' as child of 'PurchaseOrder'
v_text = text-003. " Order
v_value = w_xml-purchaseorder.
v_element_ebeln = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_poid ).
Create element 'OrderDate' as child of 'PurchaseOrder'
v_text = text-004. " Order Date
v_value = v_orderdate.
v_element_aedat = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_poid ).
Create element 'Vendor' as child of 'PurchaseOrder'
v_text = text-005. " Supplier
v_value = v_supplier.
v_element_lifnr = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_poid ).
Create element 'origin' as child of 'PurchaseOrder'
v_text = text-006. " Origin
v_value = v_origin.
v_element_ekgrp = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_poid ).
Create element 'Buying Agent' as child of 'PurchaseOrder'
v_text = text-007.
v_value = v_buyingagent. " Buying Agent
v_element_buyer = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_poid ).
Create element 'Mode' as child of 'PurchaseOrder'
v_text = text-008. " Mode
v_value = v_mode.
v_element_mode = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_poid ).
Create element 'ShipDate' as child of 'PurchaseOrder'
v_text = text-009. " Ship Date
v_value = v_shipdate.
v_element_zzshpdat = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_poid ).
Create element'Buyingterm' as child of 'PurchaseOrder'
v_text = text-010. " Buying Terms
v_value = v_buyingterms.
v_element_zterm = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_poid ).
********* Filling up header fields ******* END
Create element 'LineItemsCollection' as child of 'PurchaseOrder'
v_text = text-011. " Line items Collection
v_element_lines = v_document->create_simple_element(
name = v_text
parent = v_element_poid ).
ENDAT.
AT NEW lineitem.
Create element 'LineItem' as child of 'LineItemsCollection'
v_text = text-012. " Line Item
v_element_line = v_document->create_simple_element(
name = v_text
parent = v_element_lines ).
ENDAT.
Create element 'Line' as child of 'LineItem'
v_text = text-013. " Line
v_value = w_xml-lineitem.
v_element_item = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_line ).
Create attribute 'matnr' as child of 'LineItem'
v_text = text-014. " Product Code
v_value = w_xml-productcode.
v_element_matnr = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_line ).
Create attribute 'description' as child of 'LineItem'
v_text = text-015. " Description
v_value = w_xml-productdescription.
v_element_makt = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_line ).
Create attribute 'quantity' as child of 'LineItem'
v_text = text-016. " Quantity
v_value = w_xml-quantity.
v_element_menge = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_line ).
Create attribute 'warehouse' as child of 'LineItem'
v_text = text-017. " Warehouse
v_value = w_xml-warehouse.
v_element_wareh = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_line ).
Create attribute 'Color' as child of 'LineItem'
v_text = text-018. " Color
v_value = w_xml-color.
v_element_color = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_line ).
Create attribute 'size' as child of 'LineItem'
v_text = text-019. " Size
v_value = w_xml-productsize.
v_element_size = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_line ).
Create attribute 'netpr' as child of 'LineItem'
v_text = text-020. " Price
v_value = w_xml-price.
v_element_netpr = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_line ).
Create attribute 'orderstatus' as child of 'LineItem'
v_text = text-021. " Order Status
v_value = w_xml-orderstatus.
v_element_ordst = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_line ).
Create attribute 'Deliverydate' as child of 'LineItem'
v_text = text-022. " Delivery Date
v_value = v_deliverydate.
v_element_delvdt = v_document->create_simple_element(
name = v_text
value = v_value
parent = v_element_line ).
CLEAR w_xml.
ENDLOOP.
Creating a stream factory
v_streamfactory = v_ixml->create_stream_factory( ).
Connect internal XML table to stream factory
v_ostream = v_streamfactory->create_ostream_itable( table = i_xml_table ).
Rendering the document
v_renderer = v_ixml->create_renderer( ostream = v_ostream
document = v_document ).
v_rc = v_renderer->render( ).
ENDFORM. " P_XML
&----
*& Form P_xml_SAVE
&----
Saving the XML file in the Application layer
----
FORM p_xml_save .
***********************************************************************
Saving the XML document *
***********************************************************************
*
v_xml_size = v_ostream->get_num_written_raw( ).
*
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
bin_filesize = v_xml_size
filename = 'C:\Documents and Settings\231539.tcsdz\desktop\test.xml'
filetype = 'BIN'
CHANGING
data_tab = i_xml_table
EXCEPTIONS
OTHERS = 24.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
rewards if useful..
regards,
kavitha
11-29-2007 7:57 AM
Hi Rainer
If you have not already checked this blog...
<a href="/people/r.eijpe/blog/2005/11/21/xml-dom-processing-in-abap-part-ii--convert-an-xml-file-into-an-abap-table-using-sap-dom-approach:///people/r.eijpe/blog/2005/11/21/xml-dom-processing-in-abap-part-ii--convert-an-xml-file-into-an-abap-table-using-sap-dom-approach
it was very useful when i had a similar requirement...
Regards,
Arun
08-23-2008 9:25 AM
Hi
I have the following code to translate an internal table to XML. The probelm is that when I transfer the XML to a presentation server # appear at the end of the file. I am transfering the file to a Unix server.
</Warehouse Instruction>########################################
How do I get rid of the #.
The other probelm is that the tag <item> always forms part of the XML. I don't want it to appear.
I am using the following code.
CREATE THE XML OBJECT
CREATE OBJECT L_XML.
CONVERT THE DATA TO XML
CALL METHOD L_XML->CREATE_WITH_DATA( NAME = 'WH ' DATAOBJECT =
WHINS[] ).
call method l_xml->get_as_table
importing table = lt_data
size = l_size.
OPEN DATASET outfile IN BINARY MODE.
loop at t_test.
TRANSFER t_test-t_data to outfile.
endloop.
CLOSE DATASET outfile.
Regards
Sanjay