Runtime error accessing tinyXML element

Yesterday was my first attempt. I am trying to catch the variable "time" in the following file "new.xml"

<?xml version="1.0" standalone=no> <main> <ToDo time="1"> <Item priority="1"> Go to the <bold>Toy store!</bold></Item> <Item priority="2"> Do bills</Item> </ToDo> <ToDo time="2"> <Item priority="1"> Go to the Second<bold>Toy store!</bold></Item> </ToDo> </main> 

Here is my code

 TiXmlDocument doc("new.xml"); TiXmlNode * element=doc.FirstChild("main"); element=element->FirstChild("ToDo"); string temp=static_cast<TiXmlElement *>(element)->Attribute("time"); 

But I get runtime errors from the third and fourth lines. Can anyone shed some light on this question?

+4
source share
3 answers

It seems to me that you forgot to upload the file. Usually I do something in this direction:

 TiXmlDocument doc("document.xml"); bool loadOkay = doc.LoadFile(); // Error checking in case file is missing if(loadOkay) { TiXmlElement *pRoot = doc.RootElement(); TiXmlElement *element = pRoot->FirstChildElement(); while(element) { string value = firstChild->Value(); // In your example xml file this gives you ToDo string attribute = firstChild->Attribute("time"); //Gets you the time variable element = element->NextSiblingElement(); } } else { //Error conditions } 

Hope this helps

+2
source
 #include "tinyXml/tinyxml.h" const char MY_XML[] = "<?xml version='1.0' standalone=no><main> <ToDo time='1'> <Item priority='1'> Go to the <bold>Toy store!</bold></Item> <Item priority='2'> Do bills</Item> </ToDo> <ToDo time='2'> <Item priority='1'> Go to the Second<bold>Toy store!</bold></Item> </ToDo></main>"; void main() { TiXmlDocument doc; TiXmlHandle docHandle(&doc); const char * const the_xml = MY_XML; doc.Parse(MY_XML); TiXmlElement* xElement = NULL; xElement = docHandle.FirstChild("main").FirstChild("ToDo").ToElement(); int element_time = -1; while(xElement) { if(xElement->QueryIntAttribute("time", (int*)&element_time) != TIXML_SUCCESS) throw; xElement = xElement->NextSiblingElement(); } } 

How it works. Compiled and tested.
Since you can see that your attempts to make code with extra code cost you with exceotion on your third line (question), and without testing, I can argue with this "pointing to null" exception.

Just load my style as TinyXml docs say: "docHandle.FirstChild (" main "). FirstChild (" ToDo "). ToElement ();".

Hope this helps you understand, let me know if this is not clear. I accept visas (:

0
source

Is it just me, or is the pugixml version looking much better?

 #include <iostream> #include "pugixml.hpp" using namespace std; using namespace pugi; int main() { xml_document doc; if (!doc.load_file("new.xml")) { cerr << "Could not load xml"; return 1; } xml_node element = doc.child("main"); element = element.child("ToDo"); cout << "Time: " << element.attribute("time") << endl; } 

Also new.xml had an error, not:

 <?xml version="1.0" standalone=no> 

it should be

 <?xml version="1.0" standalone="no"?> 

Compilation was just a matter of cl test.cpp pugixml.cpp

0
source

All Articles