• Advertisement

Archived

This topic is now archived and is closed to further replies.

TinyXML quick question

This topic is 5146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Given this XML file, how can i get to the data string "Washington" in C++
<?xml version="1.0" encoding="utf-8" ?>
<root>
    <fruit>
        <apples>Washington</apples>
    </fruit>
</root>
Im rather new to XML. A friend recommended to use because its useful in a lot of instances. Anway, i can navigate up to apples element, and get to it if its a attribuite of apples. But cant seem to find a way to get it if its data. Is data the term for strings between markup? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Okay;

I'll explain this quickly and in PsuedoCode which won't be the correct syntax or anything


TiXmlDocument xmlDoc;
xmlDoc->Load( filename );
char szSomeText[1024]; // holds your value


if (xmlDoc) // is valid?

{

TiXmlElement *xmlRoot, *xmlFruit, *xmlApples;



// get first sub element called 'root' in the document

xmlRoot = xmlDoc->FirstChildElement("root");
// check for validity

if (xmlRoot)
{
xmlFruit = xmlRoot->FirstChildElement("fruit");

if (xmlFruit)
{
xmlApples = xmlFruit->FirstChildElement("apples");

if (xmlApples)
{
// copy value over to the string array we set up before

strcpy( szSomeText, xmlApples->Value() );
}
}

}


}


[edited by - downgraded on January 18, 2004 8:14:38 AM]

Share this post


Link to post
Share on other sites
I think you get the child item of the <apples> tag (which is the text), and use the Value() method on it.

/** The meaning of ''value'' changes for the specific type of
TiXmlNode.
@verbatim
Document: filename of the xml file
Element: name of the element
Comment: the comment text
Unknown: the tag contents
>>>> Text: the text string <<<<
@endverbatim

The subclasses will wrap this function.
*/

Share this post


Link to post
Share on other sites
Using a handle the syntax is a bit cleaner


TiXmlDocument document;
document->LoadFile(filename);
TiXmlHandle docHandle(&document);
TiXmlElement* foo = docHandle.FirstChild( "root" ).FirstChild( "fruit" ).FirstChild( "apples").Element();
if (foo)
{
// do something with foo, like foo->Value()

}

Share this post


Link to post
Share on other sites

  • Advertisement