Need assistance with C++ function to parse/display XML serialization -
Need assistance with C++ function to parse/display XML serialization -
i need help getting xml serialization work properly. right have 2 functions read xml file, char char. right observe , store elements storing content of elements variable. have function print hierarchy of elements , show contents contain.
my problem: can't function correctly identify end tag elements! example, when parsing gets elements end tag, detects element rather end of element.
sorry text , length of code. i'm trying thorough possible. in advanced!
this displayed when code compiled , ran. notice hot there elements equal nothing:
xml.world.item.name = silver key xml.world.item.properties.property = metal xml.world.item.properties.property = silver xml.world.item.properties = xml.world.item.weight = 1 xml.world.item.displaychar = ) xml.world.item.value = 10 xml.world.item.rarity = 5 xml.world.item = xml.world.creature.name = orc xml.world.creature.properties.property = orcish xml.world.creature.properties.property = humanoid xml.world.creature.properties = xml.world.creature.level = 2 xml.world.creature.maxhp = 15 xml.world.creature.displaychar = o xml.world.creature = xml.world =
my xml file called world.xml , xml code contains:
<?xml version="1.0" encoding="utf-8"?> <world> <item> <name>silver key</name> <properties> <property>metal</property> <property>silver</property> </properties> <weight>1</weight> <displaychar>)</displaychar> <value>10</value> <rarity>5</rarity> </item> <creature> <name>orc</name> <properties> <property>orcish</property> <property>humanoid</property> </properties> <level>2</level> <maxhp>15</maxhp> <displaychar>o</displaychar> </creature> </world>
here code beingness used - xmlserialization.h, xmlserialization.cpp, , main.cpp:
xmlserialization.h
#include <iostream> #include <string> #include <fstream> class xmlserialization { public: virtual bool parseelement(std::istream & xmlfile, std::string hierarchy$ virtual bool parsexml(std::istream & xmlfile); private: //none };
xmlserialization.cpp
#include "xmlserialization.h" #include <string> using namespace std; bool xmlserialization::parseelement(istream & xmlfile, string hierarchy){ char c; // character reach string elementname; //reads char char, checking '>' @ end of tag { c = xmlfile.get(); if (c != '>') elementname.push_back(c); } while (c != '>'); string content; //holds non-element content of element while (true){ c = xmlfile.get(); if (c == '<'){ if (xmlfile.peek() == '/'){ xmlfile.get(); string endtag; //holds end tag read while(c != '>'){ c = xmlfile.get(); if (c != '>'){ endtag.push_back(c); } } if (endtag != elementname){ cout<<"tag name mismatch! "<<endtag<< " differs "<<elementname <<"."<<endl; homecoming false; } //output known. in //file, current element, , content cout<<hierarchy<<"."<<elementname<<" = "<< content<<endl; homecoming true; } else { //read in '<' , not end tag. c @ //the first char after < function calls //on again. passing hierarchy , current //element name next element knows //is in xmlfile. if (!parseelement(xmlfile, hierarchy + "." + el$ homecoming false; } } } else { //c not '<' content. ignores eol if (c != '\n'){ content.push_back(c); } } } homecoming true; } // checks valid xml header bool xmlserialization::parsexml(istream & xmlfile){ char c; // char hold character reach //get character while character != '<' { c = xmlfile.get(); } while (c != '<'); //checks character after '<' if (xmlfile.get() != '?'){ cout<<"invalid xml header! not begin '<?'"<<endl; homecoming false; } //continues through header '?' { c = xmlfile.get(); } while (c != '?'); // checks header ending ?> if (xmlfile.get() != '>'){ cout<<"invalid xml header! not end '?>'"<<endl; homecoming false; } // go through character until first tag after header { c = xmlfile.get(); } while (c != '<'); // @ first character after opening '<' of tag // phone call parseelement homecoming parseelement(xmlfile, "xml"); }
main.cpp
#include "xmlserialization.h" #include <fstream> #include <iostream> using namespace std; int main(int argc, char * argv[]){ cout<<"________________________"<<endl; cout<<"xml testing"<<endl<<"________________________"<<endl<<endl; ifstream xmlfile; xmlfile.open("world.xml"); xmlserialization test; test.parsexml(xmlfile); xmlfile.close(); homecoming 0; }
please love of holy, utilize proper xml parser. glad did. highly recommend libxml2.
c++ xml parsing xml-parsing xml-serialization
Comments
Post a Comment