Show TOC Anfang des Inhaltsbereichs

Hintergrunddokumentation Parsing an XML document DOM-based  Dokument im Navigationsbaum lokalisieren

As mentioned earlier, parsing an XML document can be done in two ways: either creating a DOM representation of the XML document, or by the parser firing events as logical elements are encountered in a run through an XML document. This section covers DOM-based parsing.

What you will need to create a DOM representation of an XML document are the following objects:

As always, you will need the cl_ixml main factory. In addition to that we will need a cl_ixml_stream_factory object to create the input stream from. Then there must be place to store the DOM-tree in. A cl_ixml_document object is the answer. The previous section Basic steps in your program tells you how to create these objects.

In order to parse a document, you will also need a cl_ixml_parser object. The Parser can be obtained from the iXML factory by the following call:

data: parser type ref to if_ixml_parser.

parser = g_ixml->create_parser( stream_factory = streamFactory

                                istream        = iStream

                                document       = document ).

A cl_ixml_parser object is a "use once and throw away" object. That means that you create a new cl_ixml_parser, call it to parse one document and then throw the parser object away. There is no way of reusing the parser for an additional XML document.

Our goal was to parse an XML document into a DOM tree, so here we go now:

parser->parse( ).

That's it. If there haven't been any errors in the XML document we just parsed from the input stream provided, then the document object we passed to the factory method of the parser will now contain the DOM representation we were looking for.

Since errors usually happen, we should try to play this game a little bit safer, do some error checking and print out diagnostic messages:

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.

endif.

Now you can do whatever you want with the cl_ixml_documentobject you passed to the parser.

Here, for those who like copy-and-paste-style programming, the complete code fragment:

type-pools: ixml.

class cl_ixml definition load.

 

data: g_ixml type ref to if_ixml.

g_ixml = cl_ixml=>create( ).

 

data: streamFactory type ref to if_ixml_stream_factory.

streamFactory = g_ixml->createStreamFactory( ).

 

data: iStream type ref to if_ixml_istream.

data: xml_doc type string value 'hello world!'.

iStream = streamFactory->create_istream_string( xml_doc ).

 

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.

endif.



Ende des Inhaltsbereichs