Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *&---------------------------------------------------------------------*
- *& Report ZZ_DOM_PARSE_XHTML
- *&---------------------------------------------------------------------*
- * From time to time, I receive requests on how to use the XML DOM parser
- * This report should explain once and forever how this is done
- * It scans an XHTML document for paragraphs (<p> elements), using the
- * method if_ixml_document->get_elements_by_tag_name_ns( ).
- * Of course, this is only an example. For other questions, other actions
- * on the DOM might be more appropriate, for example traversing the tree
- * with methods like get_children( )
- * For collections of elements, I prefer to work with the table type
- * DCXMLELEMS, a standard table of IF_IXML_ELEMENT objects
- * In ABAP, it's easier and more readable to loop over a table than having
- * to iterate over an interface
- * Observe that the parser mode "Namespace aware" has no influence at all
- * on the result
- report zz_dom_parse_xhtml.
- data: go_xml type ref to if_ixml,
- go_stream_factory type ref to if_ixml_stream_factory.
- parameters: p_elnam type text30 default 'p',
- p_ns type text128 default 'http://www.w3.org/1999/xhtml',
- p_aware as checkbox default space.
- initialization.
- perform init.
- start-of-selection.
- perform start.
- * ---
- form start.
- data: lo_xhtml type ref to if_ixml_document,
- lt_elements type dcxmlelems,
- lv_element_name type string,
- lv_namespace type string.
- try.
- perform build_test_xhtml using p_aware changing lo_xhtml.
- lv_element_name = p_elnam.
- lv_namespace = p_ns.
- perform search_elements using lo_xhtml lv_element_name lv_namespace
- changing lt_elements.
- perform write_result using lt_elements.
- catch cx_ixml_parse_error.
- * Error log has already been written
- endtry.
- endform. "start
- * ---
- form search_elements using io_xhtml type ref to if_ixml_document
- iv_elnam type string
- iv_ns type string
- changing et_elements type dcxmlelems.
- data: lo_elements type ref to if_ixml_node_collection.
- lo_elements = io_xhtml->get_elements_by_tag_name_ns(
- name = iv_elnam
- uri = iv_ns ).
- perform element_table_from_collection
- using lo_elements
- changing et_elements.
- endform. "search_elements
- * ---
- form element_table_from_collection
- using io_elements type ref to if_ixml_node_collection
- changing et_elements type dcxmlelems.
- data: lo_iterator type ref to if_ixml_node_iterator,
- lo_node type ref to if_ixml_node,
- lo_element type ref to if_ixml_element.
- lo_iterator = io_elements->create_iterator( ).
- do.
- clear lo_node.
- lo_node = lo_iterator->get_next( ).
- if lo_node is not bound.
- exit.
- endif.
- clear lo_element.
- lo_element ?= lo_node->query_interface( ixml_iid_element ).
- if lo_element is bound.
- append lo_element to et_elements.
- endif.
- enddo.
- endform. "element_table_from_collection
- * ---
- form build_test_xhtml
- using iv_ns_aware type flag
- changing eo_xhtml type ref to if_ixml_document
- raising cx_ixml_parse_error.
- data: lv_xhtml type string.
- perform get_test_html_as_string changing lv_xhtml.
- perform parse using lv_xhtml iv_ns_aware
- changing eo_xhtml.
- endform. "build_test_xhtml
- * ---
- form parse using iv_xml type string
- iv_ns_aware type flag
- changing eo_doc type ref to if_ixml_document
- raising cx_ixml_parse_error.
- data: lo_parser type ref to if_ixml_parser,
- lo_stream type ref to if_ixml_istream.
- lo_stream = go_stream_factory->create_istream_string( iv_xml ).
- eo_doc = go_xml->create_document( ).
- lo_parser = go_xml->create_parser( document = eo_doc
- istream = lo_stream
- stream_factory = go_stream_factory ).
- if iv_ns_aware = 'X'.
- lo_parser->set_namespace_mode( if_ixml_parser=>co_namespace_aware ).
- endif.
- lo_parser->parse( ).
- if lo_parser->num_errors( ) > 0.
- perform do_parser_errors using lo_parser.
- endif.
- endform. "parse
- * ---
- form do_parser_errors
- using io_parser type ref to if_ixml_parser
- raising cx_ixml_parse_error.
- data: lo_error type ref to if_ixml_parse_error,
- lv_maxnum type i,
- lv_num type i,
- lv_text type string,
- lv_line type i,
- lv_column type i,
- lv_severity type i.
- * Fehler im Nachrichtensammler einfügen
- lv_maxnum = io_parser->num_errors( ).
- while lv_num < lv_maxnum.
- lo_error = io_parser->get_error( lv_num ).
- lv_text = lo_error->get_reason( ).
- lv_line = lo_error->get_line( ).
- lv_column = lo_error->get_column( ).
- lv_severity = lo_error->get_severity( ).
- write: / lv_line left-justified no-gap, '(' no-gap, lv_column left-justified no-gap, ')', lv_text.
- add 1 to lv_num.
- endwhile.
- * Wenn Fehler auftraten, Ausnahme auslösen
- if lv_maxnum > 0.
- raise exception type cx_ixml_parse_error
- exporting
- reason = lv_text
- line = lv_line
- column = lv_column.
- endif.
- endform. "write_parser_errors
- *---
- form init.
- go_xml = cl_ixml=>create( ).
- go_stream_factory = go_xml->create_stream_factory( ).
- endform. "init
- * ---
- form get_test_html_as_string changing ev_xhtml type string.
- data: lv_xhtml type string.
- concatenate
- `<?xml version="1.0" encoding="ISO-8859-1" ?>`
- *`<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "xhtml1-transitional.dtd">`
- `<html xmlns="http://www.w3.org/1999/xhtml">`
- `<head>`
- `<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />`
- `<title>Wenn HTML zu XHTML wird</title>`
- `</head>`
- `<body>`
- ``
- `<h1><a name="start" id="start">Wenn HTML zu XHTML wird</a></h1>`
- ``
- `<p id="erster" >Erster Paragraph.</p>`
- `<p id="zweiter">Zweiter Paragraph.</p>`
- `<p id="dritter">Na, wievielter Paragraph wohl?</p>`
- `</body>`
- `</html>`
- into ev_xhtml
- separated by cl_abap_char_utilities=>cr_lf. " Für Zeilenausgabe in Fehlermeldungen
- endform. "get_test_html_as_string
- * ---
- form write_result using it_elements type dcxmlelems.
- data: lo_element type ref to if_ixml_element,
- lv_id type string,
- lv_text type string.
- if it_elements is not initial.
- loop at it_elements into lo_element.
- lv_id = lo_element->get_attribute( 'id' ).
- lv_text = lo_element->get_content_as_string( ).
- write: / lv_id, at 15 ':', lv_text.
- endloop.
- else.
- write: / 'Keine Elemente mit diesem Namen und Namespace gefunden'.
- endif.
- endform. "write_result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement