TinyXML2 Topic is solved
TinyXML2
Hello. Would anyone be so nice and provided me some simple sample for loading xml from file using tinyxml2? I already have the file I have saved, but I can not retrieve it and process it. I renamed the xml output file to fbmrx, is it bad?
Re: TinyXML2
When I write for the file save function:
Everything is stored in the OK output file. When Load File (Other Function):
Then int and float values are fine, but const char * x_name is not loaded at all. Error Code 8. In the output file everything is saved correctly:
Code: Select all
......
XMLDocument xmlDoc;
XMLNode * pRoot = xmlDoc.NewElement("Root");
xmlDoc.InsertFirstChild(pRoot);
XMLElement * pElement = xmlDoc.NewElement("IntValue");
pElement->SetText(10);
pRoot->InsertEndChild(pElement);
pElement = xmlDoc.NewElement("FloatValue");
pElement->SetText(0.5f);
pRoot->InsertEndChild(pElement);
pElement = xmlDoc.NewElement("name");
pElement->SetText(name.ToUTF8());
pRoot->InsertEndChild(pElement);
......
Code: Select all
.....
XMLDocument xmlDoc;
xmlDoc.LoadFile(UserServer); // OK
XMLNode * pRoot = xmlDoc.FirstChild();
if (pRoot == 0) { int index = XML_ERROR_FILE_READ_ERROR; wxLogMessage(wxT("%i"), index); } // OK
//-----------------------------------------------------------
XMLElement * pElement = pRoot->FirstChildElement("IntValue");
if (pElement == 0) { int index = XML_ERROR_PARSING_ATTRIBUTE; wxLogMessage(wxT("%i"), index); } //OK
int iOutInt;
pElement->QueryIntText(&iOutInt);
//-----------------------------------------------------------
pElement = pRoot->FirstChildElement("FloatValue");
if (pElement == 0) { int index = XML_ERROR_PARSING_ATTRIBUTE; wxLogMessage(wxT("%i"), index); } //OK
float fOutFloat;
pElement->QueryFloatText(&fOutFloat);
//----------------------------------------------------------
pElement = pRoot->FirstChildElement("name");
if (pElement == 0) { int index = XML_ERROR_PARSING_ATTRIBUTE; wxLogMessage(wxT("%i"), index); } //OK
const char* x_name;
x_name = pElement->Attribute("name"); // ???
if (x_name == 0) { int index = XML_ERROR_PARSING_ATTRIBUTE; wxLogMessage(wxT("%i"), index); } // HERE: 8 !!!
wxString a_name = wxEmptyString;
a_name = wxString::FromUTF8(x_name);
//wxLogMessage(wxT("%i"), iOutInt ); // OK
// wxLogMessage(wxT("%f"), fOutFloat ); // OK
......
Code: Select all
<Root>
<IntValue>10</IntValue>
<FloatValue>0,5</FloatValue>
<name>Bob Fish</name>
</Root>
Re: TinyXML2
Hi,
Which line gives an error?
Thank you.
Which line gives an error?
Thank you.
Re: TinyXML2
Hello. The code '8' is on this line:
This is a report from tinyxml2.h:
Nowhere else the error message will appear and everything is OK.
Code: Select all
const char* x_name;
x_name = pElement->Attribute("name"); // ???
if (x_name == 0) { int index = XML_ERROR_PARSING_ATTRIBUTE; wxLogMessage(wxT("%i"), index); } // error no. 8
Code: Select all
enum XMLError {
XML_SUCCESS = 0,
XML_NO_ATTRIBUTE,
XML_WRONG_ATTRIBUTE_TYPE,
XML_ERROR_FILE_NOT_FOUND,
XML_ERROR_FILE_COULD_NOT_BE_OPENED,
XML_ERROR_FILE_READ_ERROR,
UNUSED_XML_ERROR_ELEMENT_MISMATCH, // remove at next major version
XML_ERROR_PARSING_ELEMENT,
XML_ERROR_PARSING_ATTRIBUTE,
UNUSED_XML_ERROR_IDENTIFYING_TAG, // remove at next major version
XML_ERROR_PARSING_TEXT,
XML_ERROR_PARSING_CDATA,
XML_ERROR_PARSING_COMMENT,
XML_ERROR_PARSING_DECLARATION,
XML_ERROR_PARSING_UNKNOWN,
XML_ERROR_EMPTY_DOCUMENT,
XML_ERROR_MISMATCHED_ELEMENT,
XML_ERROR_PARSING,
XML_CAN_NOT_CONVERT_TEXT,
XML_NO_TEXT_NODE,
XML_ERROR_COUNT
};
Re: TinyXML2
Hi,
OK, So you get an error passing attribute.
What is an original file you are parsing?
Thank you.
OK, So you get an error passing attribute.
What is an original file you are parsing?
Thank you.
Re: TinyXML2
I do not use any parser. I do not even use XML declarations. The output file * .xml (works even after renaming the suffix) serves only as a container for saved data. But back to the problem solved: this is a command to get text from a <name> node that is already working for me:
Everything else remains as it was...
Code: Select all
const char *x_name = pRoot->FirstChildElement("name")->GetText();
if (x_name == 0) { int index = XML_ERROR_PARSING_ATTRIBUTE; wxLogMessage(wxT("%i"), index); } // OK
wxString a_name = wxEmptyString;
a_name = wxString::FromUTF8(x_name);
- evstevemd
- Part Of The Furniture
- Posts: 2409
- Joined: Wed Jan 28, 2009 11:57 am
- Location: United Republic of Tanzania
Re: TinyXML2
Any reason for not using wxWidgets XML?
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?