iXML ABAP Objects的测试

参考资料:iXML ABAP Objects Jumpstart

*&---------------------------------------------------------------------*
*& Report  Z_XML_LIB_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  z_xml_lib_test.

TYPE-POOLS: ixml.
CLASS cl_ixml DEFINITION LOAD.

DATA: g_ixml TYPE REF TO if_ixml.
g_ixml = cl_ixml=>create( ).

TYPES: BEGIN OF xml_line,
         data(256) TYPE x,
       END OF xml_line.

DATA: xml_table TYPE TABLE OF xml_line,
      xml_table_size TYPE i,
      istream TYPE REF TO if_ixml_istream.

CALL FUNCTION 'WS_UPLOAD'
  EXPORTING
    filename                = 'c:\test.xml'
    filetype                = 'BIN'
  IMPORTING
    filelength              = xml_table_size
  TABLES
    data_tab                = xml_table
  EXCEPTIONS
    conversion_error        = 1
    file_open_error         = 2
    file_read_error         = 3
    invalid_type            = 4
    no_batch                = 5
    unknown_error           = 6
    invalid_table_width     = 7
    gui_refuse_filetransfer = 8
    customer_error          = 9
    no_authority            = 10
    OTHERS                  = 11.
IF sy-subrc <> 0.
  WRITE: AT /1 'Erros on loading xml file, return code is ', sy-subrc.
  EXIT.
ENDIF.

DATA: streamfactory TYPE REF TO if_ixml_stream_factory.
streamfactory = g_ixml->create_stream_factory( ).
istream = streamfactory->create_istream_itable( table = xml_table
                                                size  = xml_table_size ).

DATA: document TYPE REF TO if_ixml_document.
document = g_ixml->create_document( ).

DATA: parser TYPE REF TO if_ixml_parser.
parser = g_ixml->create_parser( stream_factory = streamfactory
                                istream        = istream
                                document       = document ).

IF parser->parse( ) NE 0.
  IF parser->num_errors( ) NE 0.
    DATA: parseerror TYPE REF TO if_ixml_parse_error,
          str        TYPE string,
          i          TYPE i,
          count      TYPE i,
          index      TYPE i.
    count = parser->num_errors( ).
    WRITE: count, ' parse errors have occured:'.
    index = 0.
    WHILE index < count.       parseerror = parser->get_error( index = index ).
      i = parseerror->get_line( ).
      WRITE: 'line: ', i.
      i = parseerror->get_column( ).
      WRITE: 'column: ', i.
      str = parseerror->get_reason( ).
      WRITE: str.
      index = index + 1.
    ENDWHILE.
  ENDIF.
  EXIT.
ENDIF.

DATA: node_value TYPE string,
      node_tag TYPE string,
      node_type TYPE i.

DATA: element TYPE REF TO if_ixml_element.
element = document->get_root_element( ).

* 输出根节点的子节点标签名称和值
DATA: child TYPE REF TO if_ixml_node.
child = element->get_first_child( ).
WHILE NOT child IS INITIAL.
  node_tag = child->get_name( ).
  WRITE: AT /1 node_tag.
  node_value = child->get_value( ).
  WRITE: node_value.

  child = child->get_next( ).
ENDWHILE.
SKIP 3.

* DFS(Depth-First-Search)遍历DOM树
DATA: iterator TYPE REF TO if_ixml_node_iterator,
      node     TYPE REF TO if_ixml_node.
iterator = document->create_iterator( ).
node = iterator->get_next( ).
WHILE NOT node IS INITIAL.
  node_type = node->get_type( ).
  IF node_type = 4.
    node_tag = node->get_name( ).
    WRITE: /1 node_tag.

    node_value = node->get_value( ).
    WRITE: node_value.
  ENDIF.

  node = iterator->get_next( ).
ENDWHILE.
SKIP 3.

* 查询出标签为item的节点,输出子节点的标签名称和值
DATA: items TYPE REF TO if_ixml_node_collection.
items = document->get_elements_by_tag_name( name = 'item' ).

iterator = items->create_iterator( ).

node = iterator->get_next( ).
WHILE NOT node IS INITIAL.
  node_tag = node->get_name( ).
  WRITE: AT /1 node_tag.
  PERFORM print_sub_node USING node.

  node = iterator->get_next( ).
ENDWHILE.

*&---------------------------------------------------------------------*
*&      Form  print_sub_node
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ROOT_NODE  text
*----------------------------------------------------------------------*
FORM print_sub_node USING root_node TYPE REF TO if_ixml_node.
  DATA: nodes TYPE REF TO if_ixml_node_list,
        iterator TYPE REF TO if_ixml_node_iterator,
        node     TYPE REF TO if_ixml_node.

  DATA: node_value TYPE string,
      node_tag TYPE string.

  WRITE AT /1 sy-uline(30).

  nodes = root_node->get_children( ).
  iterator = nodes->create_iterator( ).
* iterator code as usual:
  node = iterator->get_next( ).
  WHILE NOT node IS INITIAL.
    node_tag = node->get_name( ).
    WRITE: AT /1 node_tag.
    node_value = node->get_value( ).
    WRITE: node_value.

    node = iterator->get_next( ).
  ENDWHILE.
ENDFORM.                    "print_sub_node

测试的xml文件,保存路径为c:\test.xml

<?xml version="1.0" encoding="UTF-8"?>
<orderStatus>
  <orderNo>4711</orderNo>
  <items>
    <item>
      <partNo>0815</partNo>
      <units>4</units>
      <price currency="USD">12</price>
    </item>
    <item>
      <partNo>0910</partNo>
      <units>2</units>
      <price currency="CNY">100</price>
    </item>
  </items>
</orderStatus>

Comments are closed.