Show TOC Anfang des Inhaltsbereichs

Hintergrunddokumentation Rendering a DOM tree into an XML document  Dokument im Navigationsbaum lokalisieren

Now that we have seen how to create a DOM representation of an XML document by parsing an XML input stream or by creating a document from scratch, spent quite some time modifying the tree, we now want get the DOM tree back into an output stream - e.g. a file in your computer's file system.

Rendering a complete DOM tree

What the parser is for the incoming XML document, is the renderer for the outgoing one.

The following steps should have occured somewhere in your program already:

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->create_stream_factory( ).

...

data: document type ref to if_ixml_document.

document = g_ixml->create_document( ).

Before creating the cl_ixml_renderer, we have to create an output stream which will be used to render the DOM tree into:

data: xstr TYPE xstring,
      oStream type ref to if_ixml_ostream.

oStream = streamFactory->create_ostream_xstring ( string = xstr ).

Finally we are ready to instantiate the cl_ixml_renderer object, which can be obtained from the iXML main factory and write the DOM tree contained in document into oStream:

data: renderer type ref to if_ixml_renderer,

      rc       type i.

renderer = g_ixml->create_renderer( ostream  = oStream

                                    document = document ).

rc = renderer->render( ).

 

Rendering a DOM subtree

With the above method it's only possible to render the complete DOM tree into an output stream. This is usually absolutely sufficient.

In some rare cases you might want to render several separate subtrees into a single output stream and maintain control over what is going on.

Without creating a cl_ixml_renderer object you can render any node and optionally its child nodes into an output stream with e.g. the following statement:

comment->render( ostream = oStream  recursive = ' ').

Calling this method on a cl_ixml_document instance will result in the complete DOM tree being rendered into a well-formed XML document:

document->render( ostream = oStream  recursive = 'X' ).

Rendering into a table-based stream

When rendering the DOM into a table-based stream, i.e. the XML stream is "line-wrapped" into an unstructured internal table (based on type X), it might happen, that you encounter some data in the last filled table line which seems to be garbage. You are right with that assumption!

In order to find out where the correct data stream ends and where the "garbage" starts, you have to consider the exact length of the data written to your output stream. To get that information, you have to call the method if_ixml_ostream->get_num_written_raw(). This method will tell you how many bytes have been filled into the table. Taking this number module your table's line-length will give you the number of valid characters in the last table line.

So here's how your code should look like:

types: begin of xml_line,

        data(256) type x,

       end of xml_line.

data:  xml_table type table of xml_line,

       xml_size  type i,

       oStream   type ref to if_ixml_ostream.

 

...

oStream = streamFactory->create_ostream_itable ( xml_table );

 

document->render (ostream = oStream  recursive = 'X' ).

 

* use only the first xml_size bytes of the xml table!!

xml_size = oStream->get_num_written_raw( ).



Ende des Inhaltsbereichs