Code: Alles auswählen.
types:
begin of TY_Kopf,
ID type whatever,
include type spfli as Positionen.
types: end of TY_Kopf .
Code: Alles auswählen.
types : begin of ty_res,
indent type i,
element(64) type c,
attribute(64) type c,
value(512) type c,
end of ty_res.
data : ta_result type table of ty_res,
wa_result type ty_res.
Code: Alles auswählen.
call method o_xmlparser->simpel_parse
exporting
p_table = itab_xml
p_lang = xml_table_size
importing
p_tab = ta_result
p_error = proxy_error.
Code: Alles auswählen.
*----------------------------------------------------------------------*
* CLASS cl_xmlparser DEFINITION
*----------------------------------------------------------------------*
class cl_xmlparser definition inheriting from cl_xmlproxy.
*
public section.
**** System
*
class-methods class_constructor.
*
methods : constructor.
**** Offene
*
*-Error
types : begin of ty_error,
count type i,
line type i,
column type i,
reason(512) type c,
end of ty_error.
types : ty_errortab type standard table of ty_error.
data : ta_error type ty_errortab, " Tabelle
wa_error type ty_error. " Struktur
*-Return-Table
types : begin of ty_res,
indent type i,
element(64) type c,
attribute(64) type c,
value(512) type c,
end of ty_res.
types : ty_result type standard table of ty_res.
data : ta_table type ty_result, " Tabelle
wa_table type ty_res. " Struktur
*
methods : simpel_parse importing p_table type ty_xmltab
p_lang type i optional
p_validate type boolean optional
exporting p_tab type ty_result
p_error like sy-subrc.
**** Privat
*
private section.
*
data : l_parser type ref to if_ixml_parser,
l_istream type ref to if_ixml_istream,
l_node type ref to if_ixml_node,
node type ref to if_ixml_node,
iterator type ref to if_ixml_node_iterator,
nodemap type ref to if_ixml_named_node_map,
attr type ref to if_ixml_node,
l_root_node type ref to if_ixml_node,
l_next_node type ref to if_ixml_node,
l_iterator type ref to if_ixml_node_iterator.
data : l_xmldata type string.
data : l_name type string,
name type string,
prefix type string,
value type string,
indent type i,
count type i,
index type i.
*
methods : size importing proxy_table type ty_xmltab
returning value(l_xml_size) type i.
*
endclass. "cl_xmlparser DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_xmlparser IMPLEMENTATION
*----------------------------------------------------------------------*
class cl_xmlparser implementation.
**** System
method class_constructor.
endmethod. "class_constructor
*
method constructor.
call method super->constructor.
refresh : ta_table, ta_error.
clear : wa_table, wa_error.
endmethod. "constructor
**** Privat
*
method size.
constants : offset type i value 2.
data : l_xml_wa type ty_xml.
field-symbols: <fs> type any.
clear l_xmldata .
loop at proxy_table into l_xml_wa.
assign l_xml_wa to <fs> casting type c.
concatenate l_xmldata <fs> into l_xmldata .
endloop.
l_xml_size = strlen( l_xmldata ) + offset.
clear l_xmldata .
endmethod. "size
**** Offene
*
method simpel_parse.
*
define upchar.
translate &1 to upper case.
end-of-definition.
*
clear p_error.
*-Size of document
if not p_lang is initial.
l_xml_size = p_lang.
else.
l_xml_size = me->size( proxy_table = p_table ).
endif.
check l_xml_size > 0.
*-Creating the main iXML factory
l_ixml = cl_ixml=>create( ).
*-Creating a stream factory
l_streamfactory = l_ixml->create_stream_factory( ).
*-wrap the table containing the file into a stream
l_istream = l_streamfactory->create_istream_itable( table =
p_table size = l_xml_size ).
*-Creating a document
l_document = l_ixml->create_document( ).
*-Create a Parser
l_parser = l_ixml->create_parser( stream_factory = l_streamfactory
istream = l_istream
document = l_document ).
*-Validate a document - Optional
if p_validate = true.
l_parser->set_validating( mode = if_ixml_parser=>co_validate ).
endif.
*-Parse the stream
if l_parser->parse( ) ne 0.
if l_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.
clear wa_error.
count = l_parser->num_errors( ).
wa_error-count = count.
index = 0.
while index < count.
parseerror = l_parser->get_error( index = index ).
i = parseerror->get_line( ).
wa_error-line = i.
i = parseerror->get_column( ).
wa_error-column = i.
str = parseerror->get_reason( ).
wa_error-reason = str+0(512).
index = index + 1.
endwhile.
endif.
endif.
*-Process the document
if l_parser->is_dom_generating( ) eq true.
*-PERFORM process_dom USING l_document.
node ?= l_document.
check not node is initial.
clear wa_table.
if node is initial.
exit.
endif.
*-create a node iterator
iterator = node->create_iterator( ).
*-get current node
node = iterator->get_next( ).
*-loop over all nodes
while not node is initial.
if wa_table-indent is initial.
wa_table-indent = node->get_height( ).
endif.
case node->get_type( ).
when if_ixml_node=>co_node_element.
*-element node
name = node->get_name( ).
nodemap = node->get_attributes( ).
wa_table-element = name.
upchar wa_table-element.
if not nodemap is initial.
*-attributes
count = nodemap->get_length( ).
do count times.
index = sy-index - 1.
attr = nodemap->get_item( index ).
name = attr->get_name( ).
prefix = attr->get_namespace_prefix( ).
value = attr->get_value( ).
wa_table-attribute = name.
upchar wa_table-attribute.
wa_table-value = value.
enddo.
endif.
when if_ixml_node=>co_node_text or
if_ixml_node=>co_node_cdata_section.
*-text node
value = node->get_value( ).
wa_table-value = value.
endcase.
*-advance to next node
if not wa_table-value is initial.
append wa_table to ta_table.
clear wa_table.
endif.
node = iterator->get_next( ).
endwhile.
endif.
if ta_error[] is initial.
p_tab[] = ta_table[].
else.
p_error = '1'.
refresh p_tab.
endif.
free ta_table.
endmethod. "simpel_parse
endclass. "cl_xmlparser IMPLEMENTATION