XML Writer/Reader

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

XML Writer/Reader

Post by sudi »

Did anyone notice that when writing a xmlfile with the xmlWriter the xmlReader can't read it?

System:
Ubuntu 9.10 64bit

EDIT: i see its already on the tracker....

example:

Code: Select all

#include <irrlicht.h>

class CClientConnectionData
{
public:
    class Connection
    {
    public:
        Connection(void)
        {
            Port = 0;
        }
        Connection(const Connection& con)
        {
            IP          = con.IP;
            Port        = con.Port;
            Nick        = con.Nick;
            Password    = con.Password;
        }
        Connection(const irr::c8* ip, const irr::s32& port, const irr::c8* nick, const irr::c8* password)
        {
            IP          = ip;
            Port        = port;
            Nick        = nick;
            Password    = password;
        }
        irr::core::stringc IP;
        irr::s32 Port;
        irr::core::stringc Nick;
        irr::core::stringc Password;
        Connection& operator=(const Connection& con)
        {
            IP          = con.IP;
            Port        = con.Port;
            Nick        = con.Nick;
            Password    = con.Password;

            return *this;
        }
    };
    CClientConnectionData(irr::io::IFileSystem* system) : System(system){}
    virtual ~CClientConnectionData(){}

    irr::core::array<Connection> Connections;
    void save(const irr::c8* file)
    {
        irr::io::IXMLWriter* xml = System->createXMLWriter(file);
        xml->writeXMLHeader();
        xml->writeElement(L"Connections");

        for (irr::u32 i=0;i<Connections.size();++i)
        {
            irr::io::IAttributes* attributes = System->createEmptyAttributes();
            attributes->addString("Server", Connections[i].IP.c_str());
            attributes->addInt("Port", Connections[i].Port);
            attributes->addString("Nick", Connections[i].Nick.c_str());
            attributes->addString("Password", Connections[i].Password.c_str());
            attributes->write(xml, false, L"Connection");
            attributes->drop();
        }

        xml->writeClosingTag(L"Connections");

        xml->drop();
    }
    void load(const irr::c8* file)
    {
        Connections.clear();

        irr::io::IXMLReader* xml = System->createXMLReader(file);

        bool isConnectionsFile = false;

        //printf("ParserFormat: %s\n", xml->getParserFormat() == irr::io::ETF_UTF32_LE ? "ETF_UTF32_LE" : "wtf");
        //printf("SourceFormat: %s\n", xml->getSourceFormat() == irr::io::ETF_UTF16_LE ? "ETF_UTF16_LE" : "wtf");

        while (xml->read())
        {
            switch (xml->getNodeType())
            {
            case irr::io::EXN_COMMENT:
                //printf("irr::io::EXN_COMMENT\n");
                break;
            case irr::io::EXN_CDATA:
                //printf("irr::io::EXN_CDATA\n");
                break;
            case irr::io::EXN_NONE:
                //printf("irr::io::EXN_NONE\n");
                break;
            case irr::io::EXN_TEXT:
                //printf("irr::io::EXN_TEXT\n");
                break;
            case irr::io::EXN_UNKNOWN:
                //printf("irr::io::EXN_UNKNOWN\n");
                break;
            case irr::io::EXN_ELEMENT:
                //printf("NodeName: %s\n", xml->getNodeName());
                if (irr::core::stringc("Connections") == xml->getNodeName())
                    isConnectionsFile = true;
                else if (irr::core::stringc("Connection") == xml->getNodeName() && isConnectionsFile)
                {

                    irr::io::IAttributes* attributes = System->createEmptyAttributes();
                    attributes->read(xml, false, L"Connection");
                    Connection con;
                    con.IP = attributes->getAttributeAsString("Server");
                    con.Port = attributes->getAttributeAsInt("Port");
                    con.Nick = attributes->getAttributeAsString("Nick");
                    con.Password = attributes->getAttributeAsString("Password");
                    Connections.push_back(con);
                    attributes->drop();
                }
                break;
            case irr::io::EXN_ELEMENT_END:
                if (irr::core::stringc("Connections") == xml->getNodeName())
                    isConnectionsFile = false;
                break;
            }
        }

        xml->drop();
    }
    void print(void)
    {
        printf("Connections[%i]:\n", Connections.size());
        for (irr::u32 i=0;i<Connections.size();++i)
        {
            printf("\tServer: %s\n", Connections[i].IP.c_str());
            printf("\tPort: %i\n", Connections[i].Port);
            printf("\tNick: %s\n", Connections[i].Nick.c_str());
            printf("\tPassword: %s\n", Connections[i].Password.c_str());
        }
    }
protected:
    irr::io::IFileSystem* System;
private:
};

int main(void)
{
    irr::IrrlichtDevice* device = irr::createDevice(irr::video::EDT_NULL);

    irr::io::IFileSystem* system = device->getFileSystem();

    CClientConnectionData data(system);

    data.Connections.push_back(CClientConnectionData::Connection("localhost", 5, "sudi", "blubb"));
    data.Connections.push_back(CClientConnectionData::Connection("localhost", 5, "master", "haha"));

    data.save("test.xml");

    data.load("test.xml");
    data.print();

    data.load("t.xml");
    data.print();

    device->closeDevice();

    device->drop();

    return 0;
}
t.xml <- for testing purpose create this file yourself

Code: Select all

<?xml version="1.0"?>
<Connections>
    <Connection>
        <string name="Server" value="localhost"/>
        <int name="Port" value="5"/>
        <string name="Nick" value="Sudi"/>
        <string name="Password" value="blubb"/>
    </Connection>
    <Connection>
        <string name="Server" value="localhost"/>
        <int name="Port" value="5"/>
        <string name="Nick" value="Master"/>
        <string name="Password" value="haha"/>
    </Connection>
</Connections>
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.
CuteAlien
Admin
Posts: 9682
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

I really must try to set up some cross-compiling for 64 bit systems. I just talked with someone who said that qemu has a x86_64 emulator, so I might even be able to test it - if I figure out how to get that stuff running.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

I used this xmlwriter instead of the irrlicht one and it works perfectly :D
reading is still done with the irrlicht reader bc that way i can use the attribute loadind :D
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.
vhson13
Posts: 10
Joined: Wed Mar 17, 2010 3:39 pm

Post by vhson13 »

I got other problem, when I open IGUIFileOpenDialog, I can't create file with createXMLWriter. I don't know why, anyone knows answer?
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

file dialog changes the working directory. make sure to change back after closed the dialog, or it will try to create the file in a not existing folder :D
Image
Image
vhson13
Posts: 10
Joined: Wed Mar 17, 2010 3:39 pm

Post by vhson13 »

Thank you, B@z! You're right, I find created file at the path I choose in dialog. :D
Post Reply