Menu

developing with Tinyxml

Anonymous
2011-02-08
2024-10-05
  • Anonymous

    Anonymous - 2011-02-08

    Hello all,

    I am using TinyXml for a project I am working on, in order to serialize data to xml files.
    Here is some code I have, my question is: is there a better way to do some of this? it feels really really messy. Also, I'm getting segfaults on the Attribute("name") line.

    void Entity::Serialize(TiXmlElement* root)
    {
        TiXmlElement* ent = new TiXmlElement("entity");
        TiXmlElement *components = new TiXmlElement("components");
        TiXmlElement* component = NULL;
        TiXmlElement* contents = new TiXmlElement("contents");
        TiXmlElement* properties = new TiXmlElement("properties");
        std::list <Component*>::iterator it;
        std::list<Component*>::iterator itEnd = _components->end();
        std::list<Entity*>::iterator cit;
        std::list<Entity*>::iterator citEnd=_contents.end();
        if (_contents.size()) {
            for (cit = _contents.begin(); cit != citEnd; ++cit) {
                (*cit)->Serialize(ent);
            }
        }
        ent->LinkEndChild(contents);
        if (_components->size()) {
            for (it=_components->begin(); it != itEnd; ++it) {
                component = new TiXmlElement("component");
                component->SetAttribute("name", (*it)->GetName().c_str());
                components->LinkEndChild(component);
            }
        }
        ent->LinkEndChild(components);
        variables.Serialize(properties);
        ent->LinkEndChild(properties);
        ent->SetAttribute("name", _name.c_str());
        ent->SetAttribute("desc", _desc.c_str());
        ent->SetAttribute("script", _script.c_str());
        ent->SetAttribute("onum", _onum);
        ent->SetAttribute("type", _type);
        ent->SetAttribute("location", (_location?_location->GetOnum():0));
        root->LinkEndChild(ent);
    }
    void Entity::Deserialize(TiXmlElement* root)
    {
        TiXmlElement* components = NULL;
        TiXmlElement* component = NULL;
        TiXmlElement* obj = NULL;
        TiXmlElement* contents = NULL;
    TiXmlElement* properties = NULL;
    TiXmlNode* node = NULL;
        int loc;
    //contents
        node = root->FirstChild("contents");
    if (node)
    {
    contents = node->ToElement();
        for (node = contents->FirstChild(); node; node = node->NextSibling()) {
    obj = node->ToElement();
            Entity* object = new Entity();
            object->Deserialize(obj);
            object->SetLocation(this);
            _contents.push_back(object);
        }
    }
        node = root->FirstChild("components");
    if (node)
    {
    components = node->ToElement();
        for (node = components->FirstChild(); node; node = node->NextSibling()) {
    component = node->ToElement();
            AddComponent(world->CreateComponent(component->Attribute("name")));
        }
    }
    node = root->FirstChild("properties");
    if (node)
    {
    properties = node->ToElement();
        variables.Deserialize(properties);
    }
        _name = root->Attribute("name");
        _desc = root->Attribute("desc");
        _script = root->Attribute("script");
        root->Attribute("onum", &_onum);
        root->Attribute("type", &_type);
        root->Attribute("location", &loc);
        if (!loc) {
            _location=NULL;
        } else {
            if (_type==1) {
    //it's an object
                _location=world->GetObject(loc);
            } else if (_type==2) {
    //it's a room
                _location=world->GetRoom(loc);
            }
        }
    }
    

    The goal with this is to deserialize and serialize the data, but I would like to optomize it as well as clean it up, so that it will run better and quicker.
    Thanks,

     
  • Carlos Bernard

    Carlos Bernard - 2024-10-05

    Thanks for sharing this valuable thread, I am really impressed with your efforts.

     

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.