Class XMLPrinter

Synopsis

#include <tinyxml2.h>

class TINYXML2_LIB XMLPrinter : public XMLVisitor

Description

Printing functionality. The XMLPrinter gives you more options than the XMLDocument::Print() method.

It can:

  1. Print to memory.
  2. Print to a file you provide.
  3. Print XML without a XMLDocument.

Print to Memory


XMLPrinter printer;
doc.Print( &printer );
SomeFunction( printer.CStr() );

Print to a File

You provide the file pointer.


XMLPrinter printer( fp );
doc.Print( &printer );

Print without a XMLDocument

When loading, an XML parser is very useful. However, sometimes when saving, it just gets in the way. The code is often set up for streaming, and constructing the DOM is just overhead.

The Printer supports the streaming case. The following code prints out a trivially simple XML file without ever creating an XML document.


XMLPrinter printer( fp );
printer.OpenElement( "foo" );
printer.PushAttribute( "foo", "bar" );
printer.CloseElement();

Mentioned in

Inheritance

Ancestors: XMLVisitor

Methods

XMLPrinter OverloadConstruct the printer
~XMLPrinter
ClearBufferIf in print to memory mode, reset the buffer to the beginning.
CloseElementIf streaming, close the Element.
CompactMode
CStrIf in print to memory mode, return a pointer to the XML file in memory.
CStrSizeIf in print to memory mode, return the size of the XML file in memory
OpenElementIf streaming, start writing an element
Print
PrintSpacePrints out the space before an element
PushAttribute OverloadIf streaming, add an attribute to an open element.
PushCommentAdd a comment.
PushDeclaration
PushHeaderIf streaming, write the BOM and declaration.
PushText OverloadAdd a text node.
PushText OverloadAdd a text node from an integer.
PushText OverloadAdd a text node from an unsigned.
PushText OverloadAdd a text node from a signed 64bit integer.
PushText OverloadAdd a text node from an unsigned 64bit integer.
PushText OverloadAdd a text node from a bool.
PushText OverloadAdd a text node from a float.
PushText OverloadAdd a text node from a double.
PushUnknown
Putc
SealElementIfJustOpened
Visit OverloadVisit a text node.
Visit OverloadVisit a comment node.
Visit OverloadVisit a declaration.
Visit OverloadVisit an unknown node.
VisitEnter OverloadVisit a document.
VisitEnter OverloadVisit an element.
VisitExit OverloadVisit a document.
VisitExit OverloadVisit an element.
Write Overload

Source

Lines 2237-2371 in tinyxml2.h.

class TINYXML2_LIB XMLPrinter : public XMLVisitor
{
public:
    /** Construct the printer. If the FILE* is specified,
        this will print to the FILE. Else it will print
        to memory, and the result is available in CStr().
        If 'compact' is set to true, then output is created
        with only required whitespace and newlines.
    */
    XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
    virtual ~XMLPrinter()   {}

    /** If streaming, write the BOM and declaration. */
    void PushHeader( bool writeBOM, bool writeDeclaration );
    /** If streaming, start writing an element.
        The element must be closed with CloseElement()
    */
    void OpenElement( const char* name, bool compactMode=false );
    /// If streaming, add an attribute to an open element.
    void PushAttribute( const char* name, const char* value );
    void PushAttribute( const char* name, int value );
    void PushAttribute( const char* name, unsigned value );
    void PushAttribute( const char* name, int64_t value );
    void PushAttribute( const char* name, uint64_t value );
    void PushAttribute( const char* name, bool value );
    void PushAttribute( const char* name, double value );
    /// If streaming, close the Element.
    virtual void CloseElement( bool compactMode=false );

    /// Add a text node.
    void PushText( const char* text, bool cdata=false );
    /// Add a text node from an integer.
    void PushText( int value );
    /// Add a text node from an unsigned.
    void PushText( unsigned value );
    /// Add a text node from a signed 64bit integer.
    void PushText( int64_t value );
    /// Add a text node from an unsigned 64bit integer.
    void PushText( uint64_t value );
    /// Add a text node from a bool.
    void PushText( bool value );
    /// Add a text node from a float.
    void PushText( float value );
    /// Add a text node from a double.
    void PushText( double value );

    /// Add a comment
    void PushComment( const char* comment );

    void PushDeclaration( const char* value );
    void PushUnknown( const char* value );

    virtual bool VisitEnter( const XMLDocument& /*doc*/ );
    virtual bool VisitExit( const XMLDocument& /*doc*/ )            {
        return true;
    }

    virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
    virtual bool VisitExit( const XMLElement& element );

    virtual bool Visit( const XMLText& text );
    virtual bool Visit( const XMLComment& comment );
    virtual bool Visit( const XMLDeclaration& declaration );
    virtual bool Visit( const XMLUnknown& unknown );

    /**
        If in print to memory mode, return a pointer to
        the XML file in memory.
    */
    const char* CStr() const {
        return _buffer.Mem();
    }
    /**
        If in print to memory mode, return the size
        of the XML file in memory. (Note the size returned
        includes the terminating null.)
    */
    int CStrSize() const {
        return _buffer.Size();
    }
    /**
        If in print to memory mode, reset the buffer to the
        beginning.
    */
    void ClearBuffer( bool resetToFirstElement = true ) {
        _buffer.Clear();
        _buffer.Push(0);
        _firstElement = resetToFirstElement;
    }

protected:
    virtual bool CompactMode( const XMLElement& )   { return _compactMode; }

    /** Prints out the space before an element. You may override to change
        the space and tabs used. A PrintSpace() override should call Print().
    */
    virtual void PrintSpace( int depth );
    virtual void Print( const char* format, ... );
    virtual void Write( const char* data, size_t size );
    virtual void Putc( char ch );

    inline void Write(const char* data) { Write(data, strlen(data)); }

    void SealElementIfJustOpened();
    bool _elementJustOpened;
    DynArray< const char*, 10 > _stack;

private:
    /**
       Prepares to write a new node. This includes sealing an element that was
       just opened, and writing any whitespace necessary if not in compact mode.
     */
    void PrepareForNewNode( bool compactMode );
    void PrintString( const char*, bool restrictedEntitySet );  // prints out, after detecting entities.

    bool _firstElement;
    FILE* _fp;
    int _depth;
    int _textDepth;
    bool _processEntities;
    bool _compactMode;

    enum {
        ENTITY_RANGE = 64,
        BUF_SIZE = 200
    };
    bool _entityFlag[ENTITY_RANGE];
    bool _restrictedEntityFlag[ENTITY_RANGE];

    DynArray< char, 20 > _buffer;

    // Prohibit cloning, intentionally not implemented
    XMLPrinter( const XMLPrinter& );
    XMLPrinter& operator=( const XMLPrinter& );
};





Add Discussion as Guest

Log in