I've been reading the TinyXml documentation, and examples, but I'm having trouble getting it to read the values of elements, and attributes. I'm trying to read the values and attributes of the children if item elements. What am I doing wrong?
Here's my example todo xml file. If the way I structered it seems wrong, say something. (Wasn't really sure how to structure it)
<TodoList>
<item group="group1" title="script">
<date year="2006" month="04" day="17"/>
<text>make some script
</text>
</item>
<item group="misc" title="tinyxml">
<date year="2006" month="04" day="23"/>
<text>fix it
</text>
</item>
</TodoList>
My real code is a mess, so here's the psuedo code.
//root of document, in this case I'm taking root of item elements
itemRoot = document.root().child("todolist");
//grab first item element
curElem = itemRoot.Child("item");
//iterate through all item elements
foreach(curElem = curElem.NextSibling())
{
cout << "Item: " << title << endl;
cout << "date: " year << month << day;
cout << "text: " << text << endl;
}
Here's my current code to read the file. It's a real mess.
//Item: each child 'item' of root
//block: item/string? table
{
todo_list.clear();
//get first 'item' element
//pElem=hRoot.FirstChild( "item" ).FirstChild().Element();
//pElem=hRoot.FirstChild( "item" ).FirstChild("text").Element();
//iterate through all items
for(pElem=hRoot.FirstChild("item").Element(); pElem;
pElem->NextSiblingElement()) {
//read children 'date', and 'text'
//attributes of date
//const char* attrib = pElem->FirstChild("year")->Attribute();
//if(attrib) { cout << "item->year: attribute=" << attrib << endl; }
//text value
TiXmlElement* pCur = 0;
pCur = hRoot.Element();
if(!pCur) break;
pCur = pCur->FirstChildElement("item");
if(!pCur) break;
pCur = pCur->FirstChildElement("date");
if(!pCur) break;
if(pCur->Attribute("day")) {
cout << "\tAttribute=" << pCur->Attribute("day") << endl;
}
}
/*
//iterate through all 'item' elements
for( pElem; pElem; pElem=pElem->NextSiblingElement())
{
//append text to vector
const char* pText = pElem->GetText();
if(pText) {
cout << "key=" << pElem->Value() << ", "
<< "item: text=" << pText << endl;
}
}*/
}//end block
The output I get is repeated forever. It doesn't look like an endless loop to me. Unless next sibling doesn't ever end returning valid elements?
[tab]Attribute=17
[tab]Attribute=17
[tab]Attribute=17
[tab]Attribute=17
...etc...