<?xml version="1.0" encoding="utf-8"?>
<data xmlns="custom">
<NAME>%Name%</NAME>
<LAST_NAME>%Last Name%</LAST_NAME>
<DATE>%Date%</DATE>
</data>
*&---------------------------------------------------------------------*
*& Report ZCUSTOMXML
*&
*&---------------------------------------------------------------------*
*& Report demonstrates reading word document, creating custom xml part
*& to attach it to document.
*& Pavol Olejar 23.4.2017
*&---------------------------------------------------------------------*
REPORT zcustomxml.
DATA: lv_length TYPE i,
lt_data_tab TYPE STANDARD TABLE OF x255,
lv_docx TYPE xstring,
lv_string TYPE string,
lr_docx TYPE REF TO cl_docx_document,
lr_main TYPE REF TO cl_docx_maindocumentpart,
lr_custom_col TYPE REF TO cl_openxml_partcollection,
lr_custom TYPE REF TO cl_oxml_customxmlpart,
lv_it TYPE i,
lv_custom_xml TYPE xstring,
lv_date TYPE string.
* 1ST STEP - READ FILE AND FIND CUSTOM XML
*--------------------
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = 'C:\Test.docx'
filetype = 'BIN'
IMPORTING
filelength = lv_length
CHANGING
data_tab = lt_data_tab.
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = lv_length
IMPORTING
buffer = lv_docx
TABLES
binary_tab = lt_data_tab.
CALL METHOD cl_docx_document=>load_document
EXPORTING
iv_data = lv_docx
RECEIVING
rr_doc = lr_docx.
CHECK lr_docx IS BOUND.
lr_main = lr_docx->get_maindocumentpart( ).
lr_custom_col = lr_main->get_customxmlparts( ).
* We can have more than 1 custom xml files
* so let's find correct one and if exists
DATA(lv_size) = lr_custom_col->get_count( ).
DATA: lv_flag TYPE flag VALUE abap_false.
DO lv_size TIMES.
lv_it = sy-index - 1.
lr_custom ?= lr_custom_col->get_part( iv_index = lv_it ).
DATA(lv_xml) = lr_custom->get_data( ).
CALL FUNCTION 'CRM_IC_XML_XSTRING2STRING'
EXPORTING
inxstring = lv_xml
IMPORTING
outstring = lv_string.
FIND FIRST OCCURRENCE OF 'xmlns="custom"' IN lv_string.
IF sy-subrc EQ 0.
* our custom xml was found
lv_flag = abap_true.
EXIT.
ENDIF.
ENDDO.
* 2ND STEP - CREATE OR CHANGE CUSTOM XML
*---------------------------------------
CASE lv_flag.
WHEN abap_false. "our custom xml is not there yet so we create it
lr_custom = lr_main->add_customxmlpart( ).
CONCATENATE '<?xml version="1.0" encoding="utf-8"?>'
'<data xmlns="custom">'
'<NAME>%Name%</NAME>'
'<LAST_NAME>%Last Name%</LAST_NAME>'
'<DATE>%Date%</DATE>'
'</data>'
INTO DATA(lv_custom).
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = lv_custom
IMPORTING
buffer = lv_custom_xml.
WHEN abap_true. "we provide data for document
* lr_custom and lv_xml are correctly filled from loop
* Change data in custom XML
CALL FUNCTION 'CRM_IC_XML_XSTRING2STRING'
EXPORTING
inxstring = lv_xml
IMPORTING
outstring = lv_string.
REPLACE FIRST OCCURRENCE OF '%Name%' IN lv_string
WITH 'James'.
REPLACE FIRST OCCURRENCE OF '%Last Name%' IN lv_string
WITH 'Bond'.
CONCATENATE sy-datum+6(2) '.' sy-datum+4(2) '.' sy-datum+0(4)
INTO lv_date.
REPLACE FIRST OCCURRENCE OF '%Date%' IN lv_string
WITH lv_date.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = lv_string
IMPORTING
buffer = lv_custom_xml.
ENDCASE.
* 3RD STEP - SAVE IT
*---------------------------------------
lr_custom->feed_data( iv_data = lv_custom_xml ).
lv_docx = lr_docx->get_package_data( ).
lv_length = xstrlen( lv_docx ).
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_docx
TABLES
binary_tab = lt_data_tab.
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
bin_filesize = lv_length
filename = 'C:\Test.docx'
filetype = 'BIN'
confirm_overwrite = 'X'
CHANGING
data_tab = lt_data_tab.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
8 | |
6 | |
5 | |
4 | |
4 | |
4 | |
4 | |
4 | |
3 | |
2 |