You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Patrick Parks <pa...@whidbey.com> on 2000/10/11 04:27:15 UTC

XMLWriter class

I have started hacking on an extension to XMLFormatter that I've named
XMLWriter.  It's somewhat like the opposite of an "XMLReader".  It is rather
simple minded at this point with the following tentative structure:

class XMLWriter
{
public:
    XMLWriter( XMLFormatter * formatter ) ;
    virtual ~XMLWriter() ;

    // Emit [[<name]]
    // Writes start of new tag
    void start( const XMLCh * const name ) ;

    // Emit [[</name>]] or [[/>]]
    // Writes end of tag started by start()
    void end() ;

    // Emit [[ name="value"]]
    // Writes attribute name="value" pair
    void attr( const XMLCh * const name, const DOMString & value ) ;
    void attr( const XMLCh * const name, const XMLCh * const value ) ;
    void attr( const XMLCh * const name, int value ) ;
    void attr( const XMLCh * const name, double value ) ;

    // Emit [[value]] - may emit [[>]] prefix if necessary
    // Write value following [[<name ... >]] and before [[</name>]]
    // Multiple value() calls can be done between start() and end()
    // to compose a complex sequence of sub-value-segments
    void value( const DOMString & value ) ;
    void value( const XMLCh * const value ) ;
    void value( int value ) ;
    void value( double value ) ;
    ...

private:
    ...
} ;

Here's a simple example that emits the following structure:

  <grid>
    <pages>
      <page id="1">
        <cells>
          <cell row="7" col="3" type="text">(7,3)</cell>
          <cell row="99" col="75" type="currency">99.75</cell>
        </cells>
      </page>
      <page id="2">
        <cells/>
      </page>
    <pages>
  </grid>

The code looks like this:

  XMLWriter w( ... ) ;
  w.start( L"grid" ) ;
  w.start( L"pages" ) ;
  for( page = 0 ; page < pages ; ++page )
  {
    w.start( L"page" ) ;
    w.attr( L"id", page ) ;
    w.start( L"cells" ) ;
    for( row = 0 ; row < rows ; ++row )
    {
      for( col = 0 ; col < cols ; ++col )
      {
        ... type = getType( row, col ) ;
        ... value = getValue( row, col ) ;
        ...
        if( ... )
        {
          ...
          w.start( L"cell" ) ;        /* <cell      */
          w.attr( L"row", row ) ;     /* row="xxx"  */
          w.attr( L"col", col ) ;     /* col="xxx"  */
          w.attr( L"type", type ) ;   /* type="xxx" */
          w.value( value ) ;          /* >value     */
          w.end() ;                   /* </cell>    */
        }
      }
    }
    w.end() ;  /* </cells> */
    w.end() ;  /* </page>  */
  }
  w.end() ;  /* </pages> */
  w.end() ;  /* </grid>  */

The XMLWriter class handles indentation, ">", "/>", and "</name>"
termination and escaping (NoEscapes, AttrEscapes, or CharEscapes) based on
the context and sequence of start(), end(), attr(), and value() calls.

I have started to find this useful for emitting XML representations of
structures that are not already in a DOMDocument.  SAX enables us to reading
XML without instantiating the whole document in memory... similarly
XMLWriter enables writing XML in a more direct way.

Comments??  I'm wondering if anybody else:

    (1) Knows of something similar that already exists in xerces
or
    (2) Things this approach might be useful enough to formalize
        as part of xerces.

Patrick Parks
patrick@whidbey.com




Re: XMLWriter class

Posted by Bill Schindler <de...@bitranch.com>.
"Patrick Parks" <pa...@whidbey.com> wrote:
> Comments??  I'm wondering if anybody else:
> 
>     (1) Knows of something similar that already exists in xerces
> or
>     (2) Things this approach might be useful enough to formalize
>         as part of xerces.

There's the serializer code over in Xerces-J that works with both SAX and
DOM. It can also do indenting, handle transcoding, and even has methods for
outputting text and html. I've poked at it a bit (actually converted a
chunk of it) and it looks like it wouldn't be too terrible of a porting job.



--Bill