Page 1 of 1
TinyXML2
Posted: Thu Jul 13, 2017 7:49 am
by RobertHK
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
Posted: Thu Jul 13, 2017 3:52 pm
by RobertHK
When I write for the file save function:
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);
......
Everything is stored in the OK output file. When Load File (Other Function):
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
......
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
<Root>
<IntValue>10</IntValue>
<FloatValue>0,5</FloatValue>
<name>Bob Fish</name>
</Root>
Re: TinyXML2
Posted: Thu Jul 13, 2017 4:57 pm
by ONEEYEMAN
Hi,
Which line gives an error?
Thank you.
Re: TinyXML2
Posted: Thu Jul 13, 2017 5:34 pm
by RobertHK
Hello. The code '8' is on this line:
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
This is a report from tinyxml2.h:
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
};
Nowhere else the error message will appear and everything is OK.
Re: TinyXML2
Posted: Thu Jul 13, 2017 6:07 pm
by ONEEYEMAN
Hi,
OK, So you get an error passing attribute.
What is an original file you are parsing?
Thank you.
Re: TinyXML2
Posted: Fri Jul 14, 2017 2:36 am
by RobertHK
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:
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);
Everything else remains as it was...
Re: TinyXML2
Posted: Sun Jul 16, 2017 7:07 pm
by evstevemd
Any reason for not using wxWidgets XML?