Menu

#40 Lobotomized version of TinyXML

open
nobody
None
5
2007-07-03
2007-07-03
Zmey
No

Hello all,

You may have noticed that currently (as of version 2.5.3), TinyXml is a bit complicated piece of code.

First of all, you get strange errors if you forget to define TIXML_USE_STL symbol.

Second, you in fact have THREE interfaces to the same data:
1. FILE streams based i/o, which is supposed to be the primary interface for TinyXml. Fast, reliable, but restricts you to pretty-printed XML with 4-spaces indentation.
2. STL streams based i/o. It is a bit restricted (you cannot pretty-print you XML to a stream; you cannot load a document with more than one root entry).
3. TiXmlPrinter interface for getting in-memory images of XML output. Here you can control the indentation and pretty-printing.

FILE streams i/o is implemented separately from STL streams i/o; moreover, TiXmlPrinter interface is implemented separately and uses TiXmlVisitor interface (which is just another way of traversing the XML tree).

Bloated, huh?

So, I decided to lobotomize the whole thing in order to use ONE, CONSISTENT interface for EVERYTHING. I chose STL streams for input and output - because it offers input/output from in-memory string buffers, not only files. I also removed TiXmlPrinter and TiXmlVisitor classes. Besides all that, I amputated the TiXmlCursor-related stuff (this tracks the row/column locations for errors; did you ever use that?).
Of course, this makes the i/o interface different from original TinyXml; but who cares?

You can check for presence of TIXML_LOBOTOMIZED symbol to adapt you code for both versions of TinyXml:

#ifdef TIXML_LOBOTOMIZED
... code using lobotomized TiXml
#else
... code for original TiXml
#endif

Lobotomized TinyXml generates to about 8 Kb less code than the original TinyXml.

So, what did I do?

1. Removed TIXML_USE_STL checks (use of STL is forced). No need to #define TIXML_USE_STL anymore.
2. Replaced all operations on FILE streams with iostreams. So now you can dump XML to in-memory pools as easy as to files.
3. Removed the TiXmlString files (they were not usable in STL mode, anyway).
4. Removed TiXmlPrinter class and the Visitor interface (not needed since you can output XML to std::ostringstream).
5. Removed error location tracking (it was not too usable with streams, anyway).
6. Removed Stream*() functions; restricted operator>> to TiXmlDocument only (so now there is no redundancy: all kinds of output use the same functions).
7. Added customizable indentation and newlines (note: static data, so not thread-safe).

An example use of lobotomized TinyXml interface:

// Create a document. Nothing unusual here.
TiXmlDocument doc;
TiXmlElement item( "Item" );
item.SetAttribute( "priority", "1" );
item.SetAttribute( "distance", "far" );
doc.InsertEndChild( item );

std::stringstream s;
doc.SetIndent(""); // no indentation
doc.SetNewlines(false); // no newlines in output
s << doc; // output serialized document to stringstream (no pretty-printing).

std::ofstream s1;
s1.open("file.xml");
doc.SetIndent(" "); // indent 2 spaces
doc.SetNewlines(true); // add newlines in output
// Equivalent to doc.Print(s1);
s1 << doc; // output pretty-printed document to file.
s1.close();

TiXmlDocument doc1("file.xml"); // another doc
doc1.LoadFile(); // internally uses STL iostreams, too.

As a bonus you get Makefile.vc8 for compiling with MSVC++8 into a static library:
> nmake /f Makefile.vc8 clean all

Have fun!

Discussion

  • Zmey

    Zmey - 2007-09-19

    Updated binary-safe version (uses std::string).

     
  • Zmey

    Zmey - 2007-09-19

    Logged In: YES
    user_id=1835005
    Originator: YES

    Here is the updated version.

    Changes:

    - compiled as a static library (tested on Linux and Win32/MSVC 2005).
    - uses generic callbacks for writing XML text (ready-made callbacks are provided for the most common output methods: FILE*, std::istream, std::string).
    - interface changed to use const std::string& for all input and output parameters in stead of const char*. The consequence is simple: TinyXML can now work with embedded NULL characters ('\0').
    File Added: tinyxml_ltb_253.tgz

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.