Archived

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

Amnesty

TinyXML quick question

Recommended Posts

Amnesty    174
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
evolutional    1393
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
mattd    1078
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
Rule    122
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