使用ABAP操作XML DOM时,如何利用filter功能
发布日期:2021-06-30 14:06:05 浏览次数:3 分类:技术文章

本文共 4507 字,大约阅读时间需要 15 分钟。

REPORT z_xml_filter.PARAMETERS: uuid TYPE string OBLIGATORY DEFAULT 'FA163EEF573D1ED3B795C1AA63F43F3E'.DATA: lo_ixml      TYPE REF TO if_ixml.DATA: streamfactory TYPE REF TO if_ixml_stream_factory.DATA: istream        TYPE REF TO if_ixml_istream.DATA: rv_attr_path TYPE string.DATA: document TYPE REF TO if_ixml_document.DATA: parser TYPE REF TO if_ixml_parser.DATA: xmlcontent TYPE xstring.DATA: filter_node_name     TYPE REF TO if_ixml_node_filter,      filter_attr_uuid     TYPE REF TO if_ixml_node_filter,      filter1              TYPE REF TO if_ixml_node_filter,      lo_filtered_node     TYPE REF TO if_ixml_node,      lo_parent            TYPE REF TO if_ixml_node,      lo_filtered_iterator TYPE REF TO if_ixml_node_iterator,      lv_temp              TYPE string.DATA: attributes     TYPE REF TO if_ixml_named_node_map,      child          TYPE REF TO if_ixml_node,      index          TYPE i,      lv_attr_name   TYPE string,      lv_attr_val    TYPE string,      attribute      TYPE REF TO if_ixml_attribute,      lv_name        TYPE string,      lo_docx        TYPE REF TO cl_docx_document,      lo_main        TYPE REF TO cl_docx_maindocumentpart,      lv_docx_main   TYPE xstring,      lv_parent_name TYPE string,      lv_type        TYPE string.START-OF-SELECTION.  xmlcontent = zcl_jerry_tool=>get_file_binary_by_path( '\\TSHomeServer\TSHome$\i042416\Desktop\custom_part_before.xml').  lo_ixml = cl_ixml=>create( ).  streamfactory = lo_ixml->create_stream_factory( ).  document = lo_ixml->create_document( ).  istream = streamfactory->create_istream_xstring( string = xmlcontent ).  parser = lo_ixml->create_parser( stream_factory = streamfactory                                    istream        = istream                                    document       = document ).  parser->set_normalizing( is_normalizing = ' ' ).  ASSERT parser->parse( ) = 0.  filter_attr_uuid = document->if_ixml_node~create_filter_attribute( name = 'uuid' value = uuid ).  lo_filtered_iterator = document->if_ixml_node~create_iterator_filtered( filter_attr_uuid ).  lo_filtered_node = lo_filtered_iterator->get_next( ).  WHILE lo_filtered_node IS NOT INITIAL.    lv_temp = lo_filtered_node->get_name( ).    WRITE: / 'Filtered node name: ' , lv_temp COLOR COL_NEGATIVE.    attributes = lo_filtered_node->get_attributes( ).    index = 0.    IF attributes IS NOT INITIAL.      WHILE index < attributes->get_length( ).        child = attributes->get_item( index ).        IF child IS NOT INITIAL.          attribute ?= child->query_interface( ixml_iid_attribute ).          lv_attr_name = attribute->get_name( ).          IF lv_attr_name = 'name'.            lv_name = attribute->get_value( ).          ENDIF.          IF lv_attr_name = 'type'.            lv_type = attribute->get_value( ).          ENDIF.          WRITE: / 'filtered node attribute name: ', lv_name COLOR COL_POSITIVE, ' type: ', lv_type COLOR COL_TOTAL.          IF lv_name IS NOT INITIAL AND lv_type IS NOT INITIAL.            EXIT.          ENDIF.        ENDIF.        index = index + 1.      ENDWHILE.    ENDIF.    IF lv_type = 'H'.      CONCATENATE 'n0:' lv_name rv_attr_path INTO rv_attr_path.    ELSE.      CONCATENATE 'n0:' lv_name '/' rv_attr_path INTO rv_attr_path.    ENDIF.    WRITE:/ 'Filtered path: ', rv_attr_path COLOR COL_NORMAL.    lo_parent = lo_filtered_node->get_parent( ).    WHILE lo_parent IS NOT INITIAL.      lv_parent_name = lo_parent->get_name( ).      IF lv_parent_name = 'SAP_DATA'.        EXIT.      ENDIF.      attributes = lo_parent->get_attributes( ).      index = 0.      IF attributes IS NOT INITIAL.        WHILE index < attributes->get_length( ).          child = attributes->get_item( index ).          IF child IS NOT INITIAL.            attribute ?= child->query_interface( ixml_iid_attribute ).            lv_attr_name = attribute->get_name( ).            IF lv_attr_name = 'name'.              lv_name = attribute->get_value( ).              EXIT.            ENDIF.          ENDIF.          index = index + 1.        ENDWHILE.      ENDIF.      IF lv_type = 'H'.        CONCATENATE 'n0:' lv_name '/' rv_attr_path ':' lv_name '/' INTO rv_attr_path.        CLEAR: lv_type.      ELSE.        CONCATENATE 'n0:' lv_name '/' rv_attr_path INTO rv_attr_path.      ENDIF.      CLEAR: lv_name, lv_type.      lo_parent = lo_parent->get_parent( ).      WRITE: / 'Final path: ', rv_attr_path COLOR COL_POSITIVE.    ENDWHILE.    lo_filtered_node = lo_filtered_iterator->get_next( ).  ENDWHILE.

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

转载地址:https://jerry.blog.csdn.net/article/details/103967916 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:使用ABAP内表作为数据结构实现非波拉契数列(fibonacci)的生成
下一篇:使用ABAP代码获得底层HANA数据库视图的元数据Metadata

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月15日 03时53分42秒