How can I create a new XML file using TinyXML?

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
shurijo
Posts: 148
Joined: Sat Feb 21, 2004 3:04 am

How can I create a new XML file using TinyXML?

Post by shurijo »

I noticed that a few of you are using TinyXML and I had a question about it. I've easily been able to figure out how to parse xml documents and save changes to xml documents.

I can't find documentation on how to create a new XML document. Create a new xml document with items from my code, rather than read or edit an existing document.

Does anyone know of any resources or have some sample code that can get me going here? I've spent longer than I should on trying to figure out this task.

thanks,
Tyn
Posts: 932
Joined: Thu Nov 20, 2003 7:53 pm
Location: England
Contact:

Post by Tyn »

Just use your compiler, VC++ parses XML automatically even tho it doesn't recognise the file; somehow it recognises the carriage breaks in the file and translates it like a text file. Find an XML file, edit it in VC++, then save it as a different name but make sure the extension is XML. Test it in Internet Explorer. Easy :)

Or, since you know how to parse and save an XML file, you could write some kind of editor. Shouldn't take long, especially if you use Irrlicht to create it, you'll even get a nice window rather than DOS :D
saigumi
Posts: 921
Joined: Fri Aug 22, 2003 11:31 am
Location: St. Louis, MO USA
Contact:

Post by saigumi »

No, you don't need to do any custom outputting.

Code: Select all

bool  SaveFile (const char *filename) const 
This will save the XML in memory to a file.

When you create a new document in memory, instead of specifying a file to load, just send the new TiXMLDoc without any parameters.

Code: Select all

 [color=red] TiXmlDocument () 
  Create an empty document, that has no name. 
[/color]
 
  TiXmlDocument (const char *documentName) 
  Create a document with a name. The name of the document is also the filename of the xml. 

 
  TiXmlDocument (const std::string &documentName) 
  Constructor.  
This is the doc link: http://www.grinninglizard.com/tinyxmldo ... ument.html
Crud, how do I do this again?
shurijo
Posts: 148
Joined: Sat Feb 21, 2004 3:04 am

Post by shurijo »

Thanks. I seen that, but my XML document that is created is empty. I think I'm having issues with creating a new element.

The code works if the config.xml exists, but for some reason. I can't create that intial element/node.

Code: Select all

TiXmlElement *pElement;
TiXmlDocument xmlDoc(".//config.xml");

if(!xmlDoc.LoadFile())        // Failed to open        
   xmlDoc = TiXmlDocument();

// Get Settings Element
pElement = xmlDoc.FirstChildElement("settings");
if(!pElement)       
{    // Not found, so create new one
     pElement = new TiXmlElement("settings");
}

... set each element attribute here code ....

xmlDoc.SaveFile(".//config.xml");
Post Reply