I get the error that 'FirstChild' is not a member of TiXmlElement, but this can't be right because TiXmlElement is a subclass of TiXmlNode which defines that method.
Please help - this is driving me nuts!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
But it doesn't solve the main problem - I need to iterate through the <vertex> records and examine the child elements <position> and <normal>. I can't do that currently because I can't call FirstChild() on the vertex element.
Please help!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
etc. I think you see the form of it:
- Iterate over the Mesh
- Inner loop to iterate over the vertices
- Inner loop to iterate over the vertex
- Then use GetChildElement() to get the Position and the Normal
- Query the attributes of the Position and the Normal to get the values
Does that make sense?
lee
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Your code isn't quite correct, but it led me in the right direction. Here's the routine - seems to work ok:
--------------------------------------
TiXmlDocument doc(pFilePath);
Hi, I'm having trouble writing the parser to read the following XML. What am I doing wrong?
--------- XML ---------
<?xml version="1.0" ?>
<Mesh>
<!--Contents of Mesh-->
<Vertices>
<Vertex>
<Position x="-0.724000" y="0.776000" z="0.104000" />
<Normal x="0.000000" y="0.000000" z="0.000000" />
</Vertex>
<Vertex>
<Position x="-0.716000" y="0.655000" z="0.194000" />
<Normal x="0.000000" y="0.000000" z="0.000000" />
</Vertex>
<Vertex>
<Position x="-0.721000" y="0.608000" z="0.056000" />
<Normal x="0.000000" y="0.000000" z="0.000000" />
</Vertex>
...more Vertex records...
</Vertices>
</Mesh>
---------------------
TiXmlHandle handleDoc(&doc);
TiXmlElement* element;
TiXmlHandle handleRoot(0);
element = handleDoc.FirstChildElement().Element();
handleRoot = TiXmlHandle(element);
TiXmlElement *vertexElement = handleRoot.FirstChild("Vertices").FirstChild("Vertex").Element();
for (vertexElement; vertexElement; vertexElement = vertexElement->NextSiblingElement("Vertex"))
{
float x,y,z;
TiXmlElement* positionElement = vertexElement.FirstChild("Position").Element();
positionElement->QueryFloatAttribute("x",&x);
positionElement->QueryFloatAttribute("y",&y);
positionElement->QueryFloatAttribute("z",&z);
}
I get the error that 'FirstChild' is not a member of TiXmlElement, but this can't be right because TiXmlElement is a subclass of TiXmlNode which defines that method.
Please help - this is driving me nuts!
> element = handleDoc.FirstChildElement().Element();
element should now point to <Vertices>, so this line is probably not what you meant to do:
> handleRoot = TiXmlHandle(element);
And this is doomed to fail:
> TiXmlElement *vertexElement = handleRoot.FirstChild("Vertices")
I think you just want to do this:
TiXmlHandle handleRoot( &doc );
TiXmlElement *vertexElement = handleRoot.FirstChild("Vertices").FirstChild("Vertex").Element();
lee
Thanks Lee - that cleans up some code.
But it doesn't solve the main problem - I need to iterate through the <vertex> records and examine the child elements <position> and <normal>. I can't do that currently because I can't call FirstChild() on the vertex element.
Please help!
The docs give 2 brief examples of iteration:
http://www.grinninglizard.com/tinyxmldocs/classTiXmlNode.html#a12
The easy way to go would be to "IterateChildren" on the <Vertices>, and then examine each <Position> and <Normal> with GetChildElement().
lee
So is this a bug then? How come I can't call FirstChild() on my TiXmlElement when TiXmlElement descends from the class that defines that method?
No, there isn't a bug - I didn't read your XML carefully. Please read the code below to make sure I got it right, but the form is correct.
TiXmlElement* mesh = 0;
while ( mesh = doc->IterateChildren( "Mesh", vertex ) )
{
TiXmlElement* vertices = 0;
while ( vertices = mesh->IterateChildren( "Vertices", vertices ) ) {
TiXxmlElement* vertex = 0;
...
etc. I think you see the form of it:
- Iterate over the Mesh
- Inner loop to iterate over the vertices
- Inner loop to iterate over the vertex
- Then use GetChildElement() to get the Position and the Normal
- Query the attributes of the Position and the Normal to get the values
Does that make sense?
lee
Your code isn't quite correct, but it led me in the right direction. Here's the routine - seems to work ok:
--------------------------------------
TiXmlDocument doc(pFilePath);
if (!doc.LoadFile()) {
return;
}
TiXmlNode* mesh = 0;
while ( mesh = doc.IterateChildren( "Mesh", mesh )) {
TiXmlNode* Vertices = 0;
while ( Vertices = mesh->IterateChildren( "Vertices", Vertices )) {
TiXmlNode* Vertex = 0;
while ( Vertex = Vertices->IterateChildren( "Vertex", Vertex )) {
TiXmlElement *position = Vertex->FirstChildElement("Position");
float x,y,z;
position->QueryFloatAttribute("x",&x);
position->QueryFloatAttribute("y",&y);
position->QueryFloatAttribute("z",&z);
....do something with position
TiXmlElement *normal = Vertex->FirstChildElement("Normal");
normal->QueryFloatAttribute("x",&x);
normal->QueryFloatAttribute("y",&y);
normal->QueryFloatAttribute("z",&z);
....do something with normal
}
}
}
--------------------------------------
Thanks for developing TinyXML!