XML Parsing , XML Ayrıştırma
Aşağıdaki örnekte, Distance tagı aratılır, tag içindeki alt tagların değerleri ayrıştırılır ve bir sonraki Distance tagına geçilir. Her Distance tagını bir tablo satır olarak döndürür
form get_data using prm_response type string
changing cht_data type zbw_t_vehicledistance .
data: ls_data type zbw_s_vehicledistance.
data: l_ixml type ref to if_ixml,
l_streamfactory type ref to if_ixml_stream_factory,
l_istream type ref to if_ixml_istream,
l_parser type ref to if_ixml_parser,
l_document type ref to if_ixml_document,
lx_response type xstring.
call function `SCMS_STRING_TO_XSTRING`
exporting
text = prm_response
* MIMETYPE = ` `
* ENCODING =
importing
buffer = lx_response
exceptions
failed = 1
others = 2.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
l_ixml = cl_ixml=>create( ).
l_streamfactory = l_ixml->create_stream_factory( ).
l_istream = l_streamfactory->create_istream_xstring( string = lx_response ).
l_document = l_ixml->create_document( ).
l_parser = l_ixml->create_parser( stream_factory = l_streamfactory
istream = l_istream
document = l_document ).
* Parse the stream
if l_parser->parse( ) ne 0.
RETURN.
* message `Ayrıştırılamadı` type `E`.
exit.
endif.
data: curr_node_collection type ref to if_ixml_node_collection,
curr_node_iterator type ref to if_ixml_node_iterator,
curr_node type ref to if_ixml_node,
curr_nodemap type ref to if_ixml_named_node_map,
curr_attr type ref to if_ixml_node,
curr_node_list type ref to if_ixml_node_list,
lv_val type string,
lv_fieldname type string,
lv_length type i,
lv_indx type i.
data: lv_date_str type string.
data: lv_time_str type string.
curr_node_collection = l_document->get_elements_by_tag_name( name = `Distance` ).
curr_node_iterator = curr_node_collection->create_iterator( ).
curr_node = curr_node_iterator->get_next( ).
while curr_node is not initial.
clear ls_data.
curr_node_list = curr_node->get_children( ).
clear lv_length.
lv_length = curr_node_list->get_length( ).
do lv_length times.
lv_indx = sy-index - 1.
curr_attr = curr_node_list->get_item( lv_indx ).
if curr_attr is not initial.
lv_fieldname = curr_attr->get_name( ).
lv_val = curr_attr->get_value( ).
case lv_fieldname.
when `Record_No`. ls_data-record_no = lv_val.
when `Device_No`. ls_data-device_no = lv_val.
when `License_Plate`. ls_data-license_plate = lv_val.
when `Driver`. ls_data-driver = lv_val.
when `Distance_km`. ls_data-distance_km = lv_val.
when `Initial_Odometer_Value_km`. ls_data-initial_odometer_value_km = lv_val.
when `Initial_Odometer_Time`. ls_data-initial_odometer_time = lv_val.
when `Last_Odometer_Value_km`. ls_data-last_odometer_value_km = lv_val.
when `Last_Odometer_Time`. ls_data-last_odometer_time = lv_val.
endcase.
endif.
enddo.
if ls_data-initial_odometer_time is not initial.
"2023-01-30T00:01:54+03:00
lv_date_str = ls_data-initial_odometer_time+0(10).
replace all occurrences of `-` in lv_date_str with ``.
ls_data-init_date = lv_date_str.
lv_time_str = ls_data-initial_odometer_time+11(8).
replace all occurrences of `:` in lv_time_str with ``.
ls_data-init_time = lv_time_str.
endif.
if ls_data-last_odometer_time is not initial.
lv_date_str = ls_data-last_odometer_time+0(10).
replace all occurrences of `-` in lv_date_str with ``.
ls_data-last_date = lv_date_str.
lv_time_str = ls_data-last_odometer_time+11(8).
replace all occurrences of `:` in lv_time_str with ``.
ls_data-last_time = lv_time_str.
endif.
append ls_data to cht_data.
curr_node = curr_node_iterator->get_next( ).
endwhile.
delete cht_data where RECORD_NO is INITIAL. "Toplam satırını sil
endform.