XML Writer/Reader

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

XML Writer/Reader

Post by sudi »

I didn't like the way the Irrlicht XMLwriter/reader worked and actually the writer didn't work at all for me(linux64). so wrote a wrapper for irrxml and a xmlwriter i found on the net.

Download

here a example usage for reading the config.xml in the media folder.

Code: Select all

#include "XMLread.h"
int main(int argc, char* argv[])
{
    XMLread read("media/config.xml"); //parsing is actually done now....
    
    //there are two ways getting the rootnode either call
    XMLread::Node* RootNode = read.getRoot();
    //or you call
    RootNode = read.getNode("config");  ///if this returns NULL the rootNode is not called "config" and none of its children
                                        ///this also returns the first children of the rootNode called "config" if root is not called "config"
    ///now lets get the startup model
    XMLread::Node* StartUpModelNode = RootNode->getNode("startUpModel");
    ///print the model name
    printf("Model used: %s\n", StartUpModelNode->getValue("file"));
    
    ///now get the messageText
    XMLread::Node* messageTextNode = RootNode->getNode("messageText");
    ///print the text with captn
    printf("Info Text[]: %s\n", messageTextNode->getValue("caption"), messageTextNode->Text.c_str());
    
    ///cleanup is automatic when XMLread goes out of scope

    return 0;
}

Code: Select all

#include "XMLread.h"
int main(int argc, char* argv[])
{
    XMLread writer(""); ///create the writer without loading a file first

    XMLread::Node* RootNode = writer.addNode("XMLConfig"); /// calling this function just with a name clears the xml tree and returns the root
    ///add a comment before the note starts
    RootNode->AddComment("This is a XMLConfig file");

    XMLread::Node* SomeNode = RootNode->addNode("Info");
    SomeNode->AddComment("This is the info of the file");
    ///adding a attribute
    SomeNode->AddAttribute("name", "Irrlicht 1.7.1");
    SomeNode->Text = "The Irrlicht Engine is a 3D graphics engine without gameengine components";

    ///add a datanode
    SomeNode = RootNode->addNode("Data");
    SomeNode->AddComment("This a node with Data");
    ///this is a CDATA field
    SomeNode->CData = "Some data whatever";


    ///add a node with children
    SomeNode = RootNode->addNode("Childrens");
    SomeNode->addNode("Children");
    SomeNode->addNode("Children")->addNode("Blubb");
    SomeNode->addNode("Children");

    ///save the xml tree to file
    writer.save("myconfig.xml");

    return 0;
}
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
zet.dp.ua
Posts: 66
Joined: Sat Jul 07, 2007 8:10 am

Post by zet.dp.ua »

Thanks.
SAX approach is the fastest, but DOM is the simplest for use.
onesixtyfourth
Posts: 21
Joined: Mon Nov 03, 2008 10:18 am

Post by onesixtyfourth »

Have you ever tried tinyxml
Glasys
Noli illegitimi carborundum
netpipe
Posts: 669
Joined: Fri Jun 06, 2008 12:50 pm
Location: Edmonton, Alberta, Canada
Contact:

Re: XML Writer/Reader

Post by netpipe »

can you reupload this please.
Live long and phosphor!
-- https://github.com/netpipe/Luna Game Engine Status 95%
Post Reply