删除指定xml节点下所有子节点的ABAP代码

METHOD DELETE_XML_NODE.

***********************************************************************

*  This method deletes all tags which belong to a given XML node

***********************************************************************

 DATA: lo_converter                 TYPE REF TO cl_http_request.

 DATA: lv_xml_string        TYPE string.

 DATA: lv_node_start_search_string TYPE string.

 DATA: lv_node_end_search_string TYPE string.

 DATA: lv_node_found TYPE apc_v_indicator.

 DATA: lv_node_start    TYPE i.

 DATA: lv_node_end_start TYPE i.

 DATA: lv_node_end_length TYPE i.

 DATA: lv_node_end      TYPE i.

 DATA: lv_node_length   TYPE i.

 DATA: lv_bin_sign(1) TYPE X.

 DATA: lv_bin_initial TYPE xstring.

*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* delete 'line feed, new line' from cv_xml_xstring, as it can not be

* converted to string

 lv_bin_sign = '0A'.

 REPLACE ALL OCCURRENCES OF lv_bin_sign IN cv_xml_xstring WITH lv_bin_initial IN BYTE MODE.

 TRY.

   CREATE OBJECT lo_converter

     EXPORTING

       add_c_msg = 1.

*   convert given xml xstring to string

   CALL METHOD lo_converter->if_http_entity~set_data

     EXPORTING

       data = cv_xml_xstring.

   CALL METHOD lo_converter->if_http_entity~get_cdata

     RECEIVING

       data = lv_xml_string.

*   for the node's start, we have to search for the opening tag of the given node name

   CONCATENATE '<' iv_xml_node_name

   INTO lv_node_start_search_string.

*   for the node's end, we have to search for the closing tag of the given node name

   CONCATENATE ''

   INTO lv_node_end_search_string.

*   assume that there's at least one occurence of the node

   lv_node_found = abap_true.

*   loop as the node can occur several times

   WHILE lv_node_found EQ abap_true.

     FIND lv_node_start_search_string

     IN lv_xml_string

     MATCH OFFSET lv_node_start.

     IF sy-subrc = 0. " node's opening tag found!

       FIND lv_node_end_search_string

       IN lv_xml_string

       MATCH OFFSET lv_node_end_start

       MATCH LENGTH lv_node_end_length.

       lv_node_end = lv_node_end_start + lv_node_end_length.

*       for REPLACE SECTION statement the length of the whole nodes string is needed

       lv_node_length = lv_node_end - lv_node_start.

*       delete currently found occurrence of given node from xml

       REPLACE SECTION OFFSET lv_node_start LENGTH lv_node_length OF lv_xml_string WITH ''.

     ELSE. " NO node's opening tag found! -> all occurrences of node already deleted

       lv_node_found = abap_false.

       EXIT.

     ENDIF.

   ENDWHILE.

*   convert changed xml string back to xstring

   CALL METHOD lo_converter->if_http_entity~set_cdata

     EXPORTING

       data = lv_xml_string.

   CALL METHOD lo_converter->if_http_entity~get_data

     RECEIVING

       data = cv_xml_xstring.

 ENDTRY.

ENDMETHOD.

```要获取更多Jerry的原创文章,请关注公众号"汪子熙":

删除指定xml节点下所有子节点的ABAP代码

上一篇:202. 快乐数


下一篇:配置Hyper-V Server 资源计量