TiXmlPrinter Class Reference

#include <tinyxml.h>

Inheritance diagram for TiXmlPrinter:
TiXmlVisitor

List of all members.

Public Member Functions

 TiXmlPrinter ()
virtual bool VisitEnter (const TiXmlDocument &doc)
 Visit a document.
virtual bool VisitExit (const TiXmlDocument &doc)
 Visit a document.
virtual bool VisitEnter (const TiXmlElement &element, const TiXmlAttribute *firstAttribute)
 Visit an element.
virtual bool VisitExit (const TiXmlElement &element)
 Visit an element.
virtual bool Visit (const TiXmlDeclaration &declaration)
 Visit a declaration.
virtual bool Visit (const TiXmlText &text)
 Visit a text node.
virtual bool Visit (const TiXmlComment &comment)
 Visit a comment node.
virtual bool Visit (const TiXmlUnknown &unknown)
 Visit an unknow node.
void SetIndent (const char *_indent)
const char * Indent ()
 Query the indention string.
void SetLineBreak (const char *_lineBreak)
const char * LineBreak ()
 Query the current line breaking string.
void SetStreamPrinting ()
const char * CStr ()
 Return the result.
size_t Size ()
 Return the length of the result string.
const std::string & Str ()
 Return the result.

Private Member Functions

void DoIndent ()
void DoLineBreak ()

Private Attributes

int depth
bool simpleTextPrint
TIXML_STRING buffer
TIXML_STRING indent
TIXML_STRING lineBreak

Detailed Description

Print to memory functionality. The TiXmlPrinter is useful when you need to:

  1. Print to memory (especially in non-STL mode)
  2. Control formatting (line endings, etc.)

When constructed, the TiXmlPrinter is in its default "pretty printing" mode. Before calling Accept() you can call methods to control the printing of the XML document. After TiXmlNode::Accept() is called, the printed document can be accessed via the CStr(), Str(), and Size() methods.

TiXmlPrinter uses the Visitor API.

	TiXmlPrinter printer;
	printer.SetIndent( "\t" );

	doc.Accept( &printer );
	fprintf( stdout, "%s", printer.CStr() );
	

Definition at line 1712 of file tinyxml.h.


Constructor & Destructor Documentation

TiXmlPrinter::TiXmlPrinter (  )  [inline]

Definition at line 1715 of file tinyxml.h.

01715                        : depth( 0 ), simpleTextPrint( false ),
01716                                          buffer(), indent( "    " ), lineBreak( "\n" ) {}


Member Function Documentation

const char* TiXmlPrinter::CStr (  )  [inline]

Return the result.

Definition at line 1750 of file tinyxml.h.

References buffer.

01750 { return buffer.c_str(); }

void TiXmlPrinter::DoIndent (  )  [inline, private]

Definition at line 1760 of file tinyxml.h.

References buffer, depth, and indent.

Referenced by Visit(), VisitEnter(), and VisitExit().

01760                         {
01761                 for( int i=0; i<depth; ++i )
01762                         buffer += indent;
01763         }

void TiXmlPrinter::DoLineBreak (  )  [inline, private]

Definition at line 1764 of file tinyxml.h.

References buffer, and lineBreak.

Referenced by Visit(), VisitEnter(), and VisitExit().

01764                            {
01765                 buffer += lineBreak;
01766         }

const char* TiXmlPrinter::Indent (  )  [inline]

Query the indention string.

Definition at line 1734 of file tinyxml.h.

References indent.

01734 { return indent.c_str(); }

const char* TiXmlPrinter::LineBreak (  )  [inline]

Query the current line breaking string.

Definition at line 1741 of file tinyxml.h.

References lineBreak.

01741 { return lineBreak.c_str(); }

void TiXmlPrinter::SetIndent ( const char *  _indent  )  [inline]

Set the indent characters for printing. By default 4 spaces but tab () is also useful, or null/empty string for no indentation.

Definition at line 1732 of file tinyxml.h.

References indent.

01732 { indent = _indent ? _indent : "" ; }

void TiXmlPrinter::SetLineBreak ( const char *  _lineBreak  )  [inline]

Set the line breaking string. By default set to newline (
). Some operating systems prefer other characters, or can be set to the null/empty string for no indenation.

Definition at line 1739 of file tinyxml.h.

References lineBreak.

01739 { lineBreak = _lineBreak ? _lineBreak : ""; }

void TiXmlPrinter::SetStreamPrinting (  )  [inline]

Switch over to "stream printing" which is the most dense formatting without linebreaks. Common when the XML is needed for network transmission.

Definition at line 1746 of file tinyxml.h.

References indent, and lineBreak.

01746                                                                                 { indent = "";
01747                                                                                                           lineBreak = "";
01748                                                                                                         }       

size_t TiXmlPrinter::Size ( void   )  [inline]

Return the length of the result string.

Definition at line 1752 of file tinyxml.h.

References buffer.

01752 { return buffer.size(); }

const std::string& TiXmlPrinter::Str (  )  [inline]

Return the result.

Definition at line 1756 of file tinyxml.h.

01756 { return buffer; }

bool TiXmlPrinter::Visit ( const TiXmlUnknown  )  [virtual]

Visit an unknow node.

Reimplemented from TiXmlVisitor.

Definition at line 1857 of file tinyxml.cpp.

References buffer, DoIndent(), DoLineBreak(), and TiXmlNode::Value().

01858 {
01859         DoIndent();
01860         buffer += "<";
01861         buffer += unknown.Value();
01862         buffer += ">";
01863         DoLineBreak();
01864         return true;
01865 }

bool TiXmlPrinter::Visit ( const TiXmlComment  )  [virtual]

Visit a comment node.

Reimplemented from TiXmlVisitor.

Definition at line 1846 of file tinyxml.cpp.

References buffer, DoIndent(), DoLineBreak(), and TiXmlNode::Value().

01847 {
01848         DoIndent();
01849         buffer += "<!--";
01850         buffer += comment.Value();
01851         buffer += "-->";
01852         DoLineBreak();
01853         return true;
01854 }

bool TiXmlPrinter::Visit ( const TiXmlText  )  [virtual]

Visit a text node.

Reimplemented from TiXmlVisitor.

Definition at line 1813 of file tinyxml.cpp.

References buffer, TiXmlText::CDATA(), DoIndent(), DoLineBreak(), simpleTextPrint, and TiXmlNode::Value().

01814 {
01815         if ( text.CDATA() )
01816         {
01817                 DoIndent();
01818                 buffer += "<![CDATA[";
01819                 buffer += text.Value();
01820                 buffer += "]]>";
01821                 DoLineBreak();
01822         }
01823         else if ( simpleTextPrint )
01824         {
01825                 buffer += text.Value();
01826         }
01827         else
01828         {
01829                 DoIndent();
01830                 buffer += text.Value();
01831                 DoLineBreak();
01832         }
01833         return true;
01834 }

bool TiXmlPrinter::Visit ( const TiXmlDeclaration  )  [virtual]

Visit a declaration.

Reimplemented from TiXmlVisitor.

Definition at line 1837 of file tinyxml.cpp.

References buffer, DoIndent(), DoLineBreak(), and TiXmlDeclaration::Print().

01838 {
01839         DoIndent();
01840         declaration.Print( 0, 0, &buffer );
01841         DoLineBreak();
01842         return true;
01843 }

bool TiXmlPrinter::VisitEnter ( const TiXmlElement ,
const TiXmlAttribute  
) [virtual]

Visit an element.

Reimplemented from TiXmlVisitor.

Definition at line 1750 of file tinyxml.cpp.

References buffer, TiXmlText::CDATA(), depth, DoIndent(), DoLineBreak(), TiXmlNode::FirstChild(), TiXmlNode::LastChild(), TiXmlAttribute::Next(), TiXmlAttribute::Print(), simpleTextPrint, TiXmlNode::ToText(), and TiXmlNode::Value().

01751 {
01752         DoIndent();
01753         buffer += "<";
01754         buffer += element.Value();
01755 
01756         for( const TiXmlAttribute* attrib = firstAttribute; attrib; attrib = attrib->Next() )
01757         {
01758                 buffer += " ";
01759                 attrib->Print( 0, 0, &buffer );
01760         }
01761 
01762         if ( !element.FirstChild() ) 
01763         {
01764                 buffer += " />";
01765                 DoLineBreak();
01766         }
01767         else 
01768         {
01769                 buffer += ">";
01770                 if (    element.FirstChild()->ToText()
01771                           && element.LastChild() == element.FirstChild()
01772                           && element.FirstChild()->ToText()->CDATA() == false )
01773                 {
01774                         simpleTextPrint = true;
01775                         // no DoLineBreak()!
01776                 }
01777                 else
01778                 {
01779                         DoLineBreak();
01780                 }
01781         }
01782         ++depth;        
01783         return true;
01784 }

bool TiXmlPrinter::VisitEnter ( const TiXmlDocument  )  [virtual]

Visit a document.

Reimplemented from TiXmlVisitor.

Definition at line 1740 of file tinyxml.cpp.

01741 {
01742         return true;
01743 }

bool TiXmlPrinter::VisitExit ( const TiXmlElement  )  [virtual]

Visit an element.

Reimplemented from TiXmlVisitor.

Definition at line 1787 of file tinyxml.cpp.

References buffer, depth, DoIndent(), DoLineBreak(), TiXmlNode::FirstChild(), simpleTextPrint, and TiXmlNode::Value().

01788 {
01789         --depth;
01790         if ( !element.FirstChild() ) 
01791         {
01792                 // nothing.
01793         }
01794         else 
01795         {
01796                 if ( simpleTextPrint )
01797                 {
01798                         simpleTextPrint = false;
01799                 }
01800                 else
01801                 {
01802                         DoIndent();
01803                 }
01804                 buffer += "</";
01805                 buffer += element.Value();
01806                 buffer += ">";
01807                 DoLineBreak();
01808         }
01809         return true;
01810 }

bool TiXmlPrinter::VisitExit ( const TiXmlDocument  )  [virtual]

Visit a document.

Reimplemented from TiXmlVisitor.

Definition at line 1745 of file tinyxml.cpp.

01746 {
01747         return true;
01748 }


Member Data Documentation

TIXML_STRING TiXmlPrinter::buffer [private]

Definition at line 1770 of file tinyxml.h.

Referenced by CStr(), DoIndent(), DoLineBreak(), Size(), Visit(), VisitEnter(), and VisitExit().

int TiXmlPrinter::depth [private]

Definition at line 1768 of file tinyxml.h.

Referenced by DoIndent(), VisitEnter(), and VisitExit().

TIXML_STRING TiXmlPrinter::indent [private]

Definition at line 1771 of file tinyxml.h.

Referenced by DoIndent(), Indent(), SetIndent(), and SetStreamPrinting().

TIXML_STRING TiXmlPrinter::lineBreak [private]

Definition at line 1772 of file tinyxml.h.

Referenced by DoLineBreak(), LineBreak(), SetLineBreak(), and SetStreamPrinting().

Definition at line 1769 of file tinyxml.h.

Referenced by Visit(), VisitEnter(), and VisitExit().


The documentation for this class was generated from the following files:

Generated on 3 Oct 2018 for loon by  doxygen 1.6.1