wxTiXmlDocument: TinyXML + wxWidgets streams

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
ahz_
In need of some credit
In need of some credit
Posts: 7
Joined: Sun Jan 16, 2005 1:35 am

wxTiXmlDocument: TinyXML + wxWidgets streams

Post by ahz_ » Sat Feb 26, 2005 8:42 pm

The only problem I noticed so far is that SaveFile() does not format the XML nicely.

wxtinyxml.cpp

Code: Select all

// This crude code is for using wxWidget streams with TinyXML.  
// This is useful, for example, for loading and saving XML directly
// into a ZIP file (with the new ZIP streams in 2.5.4).

// Copyright (c) 2005 Andrew Ziem. All rights reserved.
// This code is licensed under the three licenses:  wxWindows Library Licence, Version 3;
// Zlib license (like TinyXML); and the GNU General Public License version 2 or later.
		

#include "wxtinyxml.h"

bool wxTiXmlDocument::LoadFile( wxInputStream &istream )
{
	Clear();
	location.Clear();
	
	wxTextInputStream txt(istream);
	
	wxString data;
	
	do
        {
            const wxString s = txt.ReadLine();

            if ( istream.Eof() && s.empty() )
                break;
		
	    data += s;

        } while (1);

	Parse( wxConvCurrent->cWX2MB(data), 0, TIXML_ENCODING_UTF8 );

	if (  Error() )
		return false;
        else
		return true;
}



bool wxTiXmlDocument::SaveFile (wxOutputStream &ostream)
{
	wxTextOutputStream txt(ostream);
	
#ifdef TIXML_USE_STL
#error not implemented
#else
	TiXmlOutStream outs;
	StreamOut (&outs);
	const char *c =  outs.c_str();
	txt << wxString(c, wxConvUTF8);
#endif

}

wxtinyxml.h

Code: Select all

#ifndef __WXTINYXML_H__
#define __WXTINYXML_H__

#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include "tinyxml.h"
#include "wx/txtstrm.h"

class wxTiXmlDocument : public TiXmlDocument
{
public:
bool LoadFile( wxInputStream &istream );
bool SaveFile( wxOutputStream &ostream );

};


#endif

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post by Ryan Norton » Sun Jun 26, 2005 11:31 pm

I believe this is the link to tinyxml - http://www.grinninglizard.com/tinyxml/
[Mostly retired moderator, still check in to clean up some stuff]

ghostd0g
Experienced Solver
Experienced Solver
Posts: 58
Joined: Wed Oct 19, 2005 3:58 pm

Post by ghostd0g » Thu Nov 24, 2005 6:53 pm

jfyi, there seems to be missing a proper return statement in wxTiXmlDocument::SaveFile. anyway thx for this useful snippet. a USE_STL version would be great though ...

Post Reply