TinyXML2  [SOLVED]

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
RobertHK
I live to help wx-kind
I live to help wx-kind
Posts: 158
Joined: Sat Dec 01, 2012 6:43 am

TinyXML2

Postby RobertHK » Thu Jul 13, 2017 7:49 am

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?

RobertHK
I live to help wx-kind
I live to help wx-kind
Posts: 158
Joined: Sat Dec 01, 2012 6:43 am

Re: TinyXML2

Postby RobertHK » Thu Jul 13, 2017 3:52 pm

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>

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 1800
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: TinyXML2

Postby ONEEYEMAN » Thu Jul 13, 2017 4:57 pm

Hi,
Which line gives an error?

Thank you.

RobertHK
I live to help wx-kind
I live to help wx-kind
Posts: 158
Joined: Sat Dec 01, 2012 6:43 am

Re: TinyXML2

Postby RobertHK » Thu Jul 13, 2017 5:34 pm

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.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 1800
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: TinyXML2

Postby ONEEYEMAN » Thu Jul 13, 2017 6:07 pm

Hi,
OK, So you get an error passing attribute.
What is an original file you are parsing?

Thank you.

RobertHK
I live to help wx-kind
I live to help wx-kind
Posts: 158
Joined: Sat Dec 01, 2012 6:43 am

Re: TinyXML2  [SOLVED]

Postby RobertHK » Fri Jul 14, 2017 2:36 am

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... :D

evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 1996
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: TinyXML2

Postby evstevemd » Sun Jul 16, 2017 7:07 pm

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?
[Ubuntu 15.04/Windows 10 Pro - GCC/MinGW, CodeLite IDE et al]


Return to “General Development”

Who is online

Users browsing this forum: No registered users and 1 guest