livelaughlove
livelaughlove

Reputation: 386

selecting xml elements

i have an xml file that looks something like this

<library>
    <book name="ABC">
        <chapter val="1"/>
            ...
    </book>
            ...
    <book id="123">
        <page val="567"/>
            ...
    </book>
</library>

I am using tinyxml to parse this xml file. and I am doing this in C++. I want to select the 'chapter' element and here's my code...

TiXmlDocument doc;
TiXmlHandle XMLFileHandle( &doc);

TiXmlElement* Book1Element = XMLFileHandle.FirstChild("library").FirstChild("book").FirstChild("chapter").FirstChild.ToElement();

it works. but when i try to select the 'page' element the same way it didn't work.

TiXmlElement* Book2Element = XMLFileHandle.FirstChild("library").FirstChild("book").FirstChild("page").FirstChild.ToElement();

why is that? i have a feeling it might be because one book element has an attribute by name, and the other book element has an attribute by id.

this is my first time dealing with xml and tinyxml, i apologize if i used the wrong terminology, and if i need to further explain myself, just let me know.

Thanks in advanced.

Upvotes: 1

Views: 206

Answers (2)

reevesy
reevesy

Reputation: 3472

Because the first Child of book does not have a page element. The Second child does

Upvotes: 2

pmr
pmr

Reputation: 59811

If the XML tree is exactly as you show it, this is not going to work, because you are still selecting the first book element, which has no page child.

Upvotes: 3

Related Questions