Is there a limit in number of attributes we can have for each element ?
I've an application where after adding a lot of new attributes, tinyxml are not able to read the other ones (numbers 1 and 3 in XML example) (with longuer attributes names, TextDetec value was null...)
Here the XML and code part file used:
(sorry for the bad presentation...)
You should search the problem in your own program, rather than in TinyXml.
I've written some examples, to show you it definitely does work fine. Just the way you read and save the attributes is quite strange. Saving on a const string pointer? Chaning values? Converting implicite from const char* to const string*? You know the difference of memory mapping of std::string and const char*?
First of all how to read attributes:
-------------------------------
#include "tinyxml.h"
#include <iostream>
I've finally found the problem in my project, you were right Georg, many thanks to have take of your time for a test.
In fact, my little XML files (one file per object) are splitted/joined before parsing (each class only parse its part). And the bug was in the splitter, where the lenght line was limited. Shame on me...! ;-) The error is sometimes where you don't search.... :-(
Else, I'm using the v1.x api (2.3.3 patched), so it explains why I get std::string in return values instead of char ! (the project was started very before the v2 versions comes...)
Many thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
A question:
Is there a limit in number of attributes we can have for each element ?
I've an application where after adding a lot of new attributes, tinyxml are not able to read the other ones (numbers 1 and 3 in XML example) (with longuer attributes names, TextDetec value was null...)
Here the XML and code part file used:
(sorry for the bad presentation...)
Many thanks.
XML example :
----------
<Detecs>
<Detec VarEntreeDet1="0" ModeVarEntreeDet1="0" Entree2EnSecours="0" VarEntreeDet2="-1" ModeVarEntreeDet2="0" TpsConfirmSecours="5000" VarRetourLigne="5968" VarDefDet="-1" ModeVarDefDet="0" IntervalleDebut="3000" IntervalleFin="3000" DureeFixeIntervalle="6000" DureeVariationIntervalle="20000" TpsAlaPresJour="-1" TpsAlaAbsJour="-1" TpsAlaPresNuit="-1" TpsAlaAbsNuit="-1" TpsAlaPresParDef="0" TpsAlaAbsParDef="0" TpsMiniBoucleUnidir="100" TpsMaxiBoucleUnidir="1000" ParamEven="-1" TpsMem="0">0</Detec>
<Detec VarEntreeDet1="1" ModeVarEntreeDet1="0" Entree2EnSecours="0" VarEntreeDet2="-1" ModeVarEntreeDet2="0" TpsConfirmSecours="5000" VarRetourLigne="5969" VarDefDet="-1" ModeVarDefDet="0" IntervalleDebut="3000" IntervalleFin="3000" DureeFixeIntervalle="6000" DureeVariationIntervalle="20000" TpsAlaPresJour="-1" TpsAlaAbsJour="-1" TpsAlaPresNuit="-1" TpsAlaAbsNuit="-1" TpsAlaPresParDef="0" TpsAlaAbsParDef="0" TpsMiniBoucleUnidir="100" TpsMaxiBoucleUnidir="1000" ParamEven="-1" TpsMem="0">1</Detec>
<Detec VarEntreeDet1="2" ModeVarEntreeDet1="0" Entree2EnSecours="0" VarEntreeDet2="-1" ModeVarEntreeDet2="0" TpsConfirmSecours="5000" VarRetourLigne="5971" VarDefDet="-1" ModeVarDefDet="0" IntervalleDebut="3000" IntervalleFin="3000" DureeFixeIntervalle="6000" DureeVariationIntervalle="20000" TpsAlaPresJour="-1" TpsAlaAbsJour="-1" TpsAlaPresNuit="-1" TpsAlaAbsNuit="-1" TpsAlaPresParDef="0" TpsAlaAbsParDef="0" TpsMiniBoucleUnidir="100" TpsMaxiBoucleUnidir="1000" ParamEven="-1" TpsMem="0">3</Detec>
</Detecs>
Code part used :
----------
TiXmlDocument ParamsXml;
TiXmlNode * BlocsDetecs;
ParamsXml.LoadFile(tcNomFichierParams);
BlocsDetecs = ParamsXml.FirstChild("Detecs");
if (BlocsDetecs)
{
TiXmlElement * EleDetec;
TiXmlText * TextDetec;
int iNumCpt;
StrDetec * pDet;
const std::string * Attribut;
for (EleDetec = (TiXmlElement *)BlocsDetecs->FirstChild("Detec"); EleDetec;
EleDetec = (TiXmlElement *)EleDetec->NextSibling("Detec") )
{
TextDetec = (TiXmlText *)EleDetec->LastChild();
iNumDet = atoi(TextDetec->Value().c_str());
pDet = &Detec[ iNumDet ];
Attribut = EleDetec->Attribute("VarEntreeDet1");
if (Attribut)
pDet->iVarEntreeDetecteur1 = atoi(Attribut->c_str());
Attribut = EleDetec->Attribute("ModeVarEntreeDet1");
if (Attribut)
pDet->ucModeVarEntreeDetecteur1 = (uchar)atoi(Attribut->c_str());
...etc...
}// for (EleDetec
}//if (BlocsDetecs)
I've forgotten to tell that for saving I haven't see any problems with the same attributes list...
Bye.
You should search the problem in your own program, rather than in TinyXml.
I've written some examples, to show you it definitely does work fine. Just the way you read and save the attributes is quite strange. Saving on a const string pointer? Chaning values? Converting implicite from const char* to const string*? You know the difference of memory mapping of std::string and const char*?
First of all how to read attributes:
-------------------------------
#include "tinyxml.h"
#include <iostream>
using std::cout;
using std::endl;
int main()
{
std::string data;
// for the ease adding it to a std::string
data = "<Detecs><Detec VarEntreeDet1='0' ModeVarEntreeDet1='0' Entree2EnSecours='0' VarEntreeDet2='20' ModeVarEntreeDet2='0' TpsConfirmSecours='5000' VarRetourLigne='5968' VarDefDet='20' ModeVarDefDet='0' IntervalleDebut='3000' IntervalleFin='3000' DureeFixeIntervalle='6000' DureeVariationIntervalle='20000' TpsAlaPresJour='20' TpsAlaAbsJour='20' TpsAlaPresNuit='20' TpsAlaAbsNuit='20' TpsAlaPresParDef='0' TpsAlaAbsParDef='0' TpsMiniBoucleUnidir='100' TpsMaxiBoucleUnidir='1000' ParamEven='20' TpsMem='0'>0</Detec><Detec VarEntreeDet1='1' ModeVarEntreeDet1='0' Entree2EnSecours='0' VarEntreeDet2='20' ModeVarEntreeDet2='0' TpsConfirmSecours='5000' VarRetourLigne='5969' VarDefDet='20' ModeVarDefDet='0' IntervalleDebut='3000' IntervalleFin='3000' DureeFixeIntervalle='6000' DureeVariationIntervalle='20000' TpsAlaPresJour='20' TpsAlaAbsJour='20' TpsAlaPresNuit='20' TpsAlaAbsNuit='20' TpsAlaPresParDef='0' TpsAlaAbsParDef='0' TpsMiniBoucleUnidir='100' TpsMaxiBoucleUnidir='1000' ParamEven='20' TpsMem='0'>1</Detec><Detec VarEntreeDet1='2' ModeVarEntreeDet1='0' Entree2EnSecours='0' VarEntreeDet2='20' ModeVarEntreeDet2='0' TpsConfirmSecours='5000' VarRetourLigne='5971' VarDefDet='20' ModeVarDefDet='0' IntervalleDebut='3000' IntervalleFin='3000' DureeFixeIntervalle='6000' DureeVariationIntervalle='20000' TpsAlaPresJour='20' TpsAlaAbsJour='20' TpsAlaPresNuit='20' TpsAlaAbsNuit='20' TpsAlaPresParDef='0' TpsAlaAbsParDef='0' TpsMiniBoucleUnidir='100' TpsMaxiBoucleUnidir='1000' ParamEven='20' TpsMem='0'>3</Detec></Detecs>";
TiXmlDocument *doc = new TiXmlDocument();
TiXmlNode *root;
TiXmlElement *iternode;
TiXmlAttribute *aiter;
doc->Parse (data.c_str());
root = doc->RootElement();
for (iternode = root->FirstChild()->ToElement(); iternode; iternode = iternode->NextSibling()->ToElement())
{
cout << endl << endl << "-----NODE: " << iternode->Value() << "------" << endl;
cout << "ATTRIBUTES: " << endl;
cout << "-----------" << endl;
for (aiter = iternode->FirstAttribute(); aiter; aiter = aiter->Next())
{
cout << aiter->Name() << ": " << aiter->Value() << endl;
}
cout << "----------" << endl;
cout << "VALUE: " << iternode->FirstChild()->Value() << endl;
}
cout << endl << *doc << endl;
delete doc;
return 0;
}
---------------------------
How to add attributes to a tree?
----------------------------------------
#include "tinyxml.h"
#include <iostream>
using std::cout;
using std::endl;
int main()
{
TiXmlDocument *doc = new TiXmlDocument();
TiXmlNode *root;
TiXmlElement *iternode;
root = doc->LinkEndChild (new TiXmlElement ("Detecs"));
for (int i=0; i<3; i++)
{
iternode = root->LinkEndChild (new TiXmlElement ("Detec"))->ToElement();
iternode->SetAttribute ("VarEntreeDet1", "0");
iternode->SetAttribute ("ModeVarEntreeDet1", "0");
iternode->SetAttribute ("Entree2EnSecours","0");
iternode->SetAttribute ("VarEntreeDet2","20");
iternode->SetAttribute ("ModeVarEntreeDet2","0");
iternode->SetAttribute ("TpsConfirmSecours", "5000");
iternode->SetAttribute ("VarRetourLigne","5968");
iternode->SetAttribute ("VarDefDet","20");
iternode->SetAttribute ("ModeVarDefDet","0");
iternode->SetAttribute ("IntervalleDebut","3000");
iternode->SetAttribute ("IntervalleFin","3000");
iternode->SetAttribute ("DureeFixeIntervalle","6000");
iternode->SetAttribute ("DureeVariationIntervalle","20000");
iternode->SetAttribute ("TpsAlaPresJour","20");
iternode->SetAttribute ("TpsAlaAbsJour","20");
iternode->SetAttribute ("TpsAlaPresNuit","20");
iternode->SetAttribute ("TpsAlaAbsNuit","20");
iternode->SetAttribute ("TpsAlaPresParDef","0");
iternode->SetAttribute ("TpsAlaAbsParDef","0");
iternode->SetAttribute ("TpsMiniBoucleUnidir","100");
iternode->SetAttribute ("TpsMaxiBoucleUnidir","1000");
iternode->SetAttribute ("ParamEven","20");
iternode->SetAttribute ("TpsMem", "0");
iternode->LinkEndChild (new TiXmlText ("0"));
}
cout << endl << *doc << endl;
delete doc;
return 0;
}
-------------------------------------------
The attribute list is a circular list and can be any length (bounded only by memory.)
lee
I've finally found the problem in my project, you were right Georg, many thanks to have take of your time for a test.
In fact, my little XML files (one file per object) are splitted/joined before parsing (each class only parse its part). And the bug was in the splitter, where the lenght line was limited. Shame on me...! ;-) The error is sometimes where you don't search.... :-(
Else, I'm using the v1.x api (2.3.3 patched), so it explains why I get std::string in return values instead of char ! (the project was started very before the v2 versions comes...)
Many thanks.