XML парсер

Пример ABAP парсера XML файла следующего вида:

  1. <?xml version="1.0" encoding="windows-1251"?>
  2. <MSG FORMAT="SAP_FORMAT" SENDER="SYSTEM1" RECIPIENT="SAP">
  3. <INS
  4. CODE="TESTXXXXXX"
  5. FROMDATE="25.07.12"
  6. LONGNAME="TESTER 25"
  7. CUST_NAME="TESTER 25"
  8. CITY="ROME"
  9. TYPECODE="0"
  10. </MSG>

убил на него целый день, на будущее пригодится:

  1. PARAMETERS: p_file TYPE rlgrap-filename.
  2.  
  3. AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  4. CALL FUNCTION 'F4_FILENAME'
  5. EXPORTING
  6. field_name = 'P_FILE'
  7. IMPORTING
  8. file_name = p_file.
  9.  
  10.  
  11. type-pools: ixml.
  12. class cl_ixml definition load.
  13.  
  14. types: begin of xml_line,
  15. data(256) type x,
  16. end of xml_line.
  17.  
  18. data: xml_table type table of xml_line,
  19. xml_tablesize type i,
  20. istream type ref to if_ixml_istream.
  21. data: s_file type string.
  22. data: g_ixml type ref to if_ixml.
  23. data: streamFactory type ref to if_ixml_stream_factory.
  24. data: parser type ref to if_ixml_parser.
  25. data: document type ref to if_ixml_document.
  26.  
  27. s_file = p_file.
  28. CALL FUNCTION 'GUI_UPLOAD'
  29. exporting
  30. filename = s_file
  31. filetype = 'BIN'
  32. importing
  33. filelength = xml_tablesize
  34. data_tab = xml_table
  35. exceptions
  36. others = 11.
  37.  
  38. g_ixml = cl_ixml=>create( ).
  39. streamFactory = g_ixml->create_stream_factory( ).
  40. * wrap the table containing the file into a stream
  41. istream = streamFactory->create_istream_itable( table = xml_table
  42. size = xml_tablesize ).
  43. document = g_ixml->create_document( ).
  44. parser = g_ixml->create_parser( stream_factory = streamFactory
  45. istream = iStream
  46. document = document ).
  47.  
  48. if parser->parse( ) ne 0.
  49. if parser->num_errors( ) ne 0.
  50. data: parseError type ref to if_ixml_parse_error,
  51. str type string,
  52. i type i,
  53. count type i,
  54. index type i.
  55. count = parser->num_errors( ).
  56. write: count, ' parse errors have occured:'.
  57. index = 0.
  58. while index < count.
  59. parseError = parser->get_error( index = index ).
  60. i = parseError->get_line( ).
  61. write: 'line: ', i.
  62. i = parseError->get_column( ).
  63. write: 'column: ', i.
  64. str = parseError->get_reason( ).
  65. write: str.
  66. index = index + 1.
  67.  
  68. data: t_str type string.
  69. data: element type ref to if_ixml_element.
  70. data: child type ref to if_ixml_node.
  71. data: attributes type ref to if_ixml_named_node_map,
  72. attribute type ref to if_ixml_attribute,
  73. achild type ref to if_ixml_node.
  74.  
  75. element = document->get_root_element( ).
  76. child = element->get_first_child( ).
  77. while not child is initial.
  78. t_str = child->get_name( ).
  79. write / t_str.
  80. data: lr_nodeiterator TYPE REF TO if_ixml_node_iterator,
  81. lr_attributenode TYPE REF TO if_ixml_node.
  82. data: lc_name type string,
  83. lc_value type string.
  84.  
  85. attributes = child->get_attributes( ).
  86. if not attributes is initial.
  87. lr_nodeiterator = attributes->create_iterator( ).
  88. lr_attributenode = lr_nodeiterator->get_next( ).
  89.  
  90. WHILE lr_attributenode IS NOT INITIAL.
  91. lc_name = lr_attributenode->get_name( ).
  92. lc_value = lr_attributenode->get_value( ).
  93. lr_attributenode = lr_nodeiterator->get_next( ).
  94. write: / lc_name, '=', lc_value.
  95. child = child->get_next( ).

Комментарии