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.
voidEntity::Serialize(TiXmlElement*root){TiXmlElement*ent=newTiXmlElement("entity");TiXmlElement*components=newTiXmlElement("components");TiXmlElement*component=NULL;TiXmlElement*contents=newTiXmlElement("contents");TiXmlElement*properties=newTiXmlElement("properties");std::list<Component*>::iteratorit;std::list<Component*>::iteratoritEnd=_components->end();std::list<Entity*>::iteratorcit;std::list<Entity*>::iteratorcitEnd=_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=newTiXmlElement("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);}voidEntity::Deserialize(TiXmlElement*root){TiXmlElement*components=NULL;TiXmlElement*component=NULL;TiXmlElement*obj=NULL;TiXmlElement*contents=NULL;TiXmlElement*properties=NULL;TiXmlNode*node=NULL;intloc;//contentsnode=root->FirstChild("contents");if(node){contents=node->ToElement();for(node=contents->FirstChild();node;node=node->NextSibling()){obj=node->ToElement();Entity*object=newEntity();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);}elseif(_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,
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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,
Thanks for sharing this valuable thread, I am really impressed with your efforts.