Seeking Basic Help with TinyXML

Started by
5 comments, last by adam_o 14 years, 7 months ago
Hello all, I've been having trouble getting TinyXML to work the way I would like it to. I don't understand it or the tutorials that I've read very well, so I'm basically asking for a solution, which should be very easy for anybody who's worked with it a lot. The reason I need TinyXML and not some other file reading gadget is because all of the files in question (thousands) are in XML format, and TInyXML has already been somewhat implemented into my project. The reason that I don't think the manuals are working for me is that I've generally learned programming by example, and none of the tutorials I've looked at showed the examples of the types of XML structures that I'm working with. In general, all of the data I need is stored in two ways: 1. Boolean data (flags) are stored like so:
<can-be-engaged/>
2. String or number data are stored like this:
<price integer="1500"/>
In the first case, I just need to identify that the "can-be-engaged" flag is set for this XML file and pass the information on to the program. In the second case, I need to know that the "price" for this object is set to "1500" (I don't need to know that it is an integer). The only other thing that I need to know is how to access the data if it's nested:
<attributes>
<can-be-engaged/>
</attributes>
So if I had a simple XML file:
<?xml version="1.0" encoding="utf-8"?>
<base-object>
	<attributes>
		<can-be-engaged/>
		<can-be-shot/>
	</attributes>
	<price integer="1500"/>
	<speed real="15.2"/>
</base-object>
How would I access and pass on all of the data that I need? All help is greatly appreciated and thanks in advance! adam_o
_______________________My computer stats:Xcode 3.1.2Mac OS 10.5.8---Visual C++ 2008 Express EditionWindows XP---NetBeansUbuntu 9.04---Help needed here!
Advertisement
Quote:Original post by adam_o
In the first case, I just need to identify that the "can-be-engaged" flag is set for this XML file and pass the information on to the program.

In the second case, I need to know that the "price" for this object is set to "1500" (I don't need to know that it is an integer).

I'm unfamiliar with TinyXML, but after having skimmed their examples, I'd imagine you'd do something like this:
// load the xml fileTiXmlDocument doc("your_xml_file.xml");doc.LoadFile();// scan the xml documentscanNode(doc);...// function to recursively read a node's childrenvoid scanNode(TiXmlNode* node){    // check here if node is null or has no children    // iterate the node's children    TiXmlNode* child = 0;    while(child = node->IterateChildren(child))    {        // get child element name        char* name = child->Value();        // check name for desired element        if(strcmp(name, "can-be-engaged") == 0)        {            // can-be-engaged is set        }        else if(strcmp(name, "attributes") == 0)        {            // scan children for "can-be-engaged"            scanNode(child);        }        else if(strcmp(name, "price") == 0)        {            // get "integer" attribute of price element            TiXmlText* text = child->Text();            TiXmlNode* integerNode = text->FirstChild();            // confirm here that integerNode element is named "integer"            // retrieve "integer" value            char* value = integerNode->Value(); // 1500            ...        }        ...    }}
You'll need to tweak the above code, of course, and some of those method calls may not be correct. But, it's something. That help at all?
Thanks for the start, most of your code works great. There were two errors I found in the text (one occurred twice, but I was able to fix it).

The one that I fixed was that the char* s had to be declared as const char* s.

The one that I could not fix is this:
			// get "integer" attribute of price element			TiXmlText* text = child->Text();

Giving me the following error:
Class 'TiXmlNode' has no member named 'Text'

Thanks for the start, I think I'm beginning to understand it.

I appreciate continuing help,

adam_o

_______________________My computer stats:Xcode 3.1.2Mac OS 10.5.8---Visual C++ 2008 Express EditionWindows XP---NetBeansUbuntu 9.04---Help needed here!
hi there,
i am not experiences with tinyxml at all but i just looked through some online documentation about it. and TiXmlText is class with no member function Text(). maybe you have to use the following
virtual void 	Print (FILE *cfile, int depth) const  	All TinyXml classes can print themselves to a filestream or the string class (TiXmlString in non-STL mode, std::string in STL mode.


have a nice day.

EDIT: oppps, please disregard the above , Print function is not useful for you. maybe use ToText(). you will have to go through the header and see exactly how to use it.
Go through the header? What about using google? I searched for 'TiXmlNode' and got back this page:

Docs googled FOR you *rolls eyes*

This lists:
virtual const TiXmlText * 	ToText () const 	Cast to a more defined type. Will return null if not of the requested type.
Quote:Original post by Jason Petrasko
Docs googled FOR you *rolls eyes*

This is the main reference I have been using in trying to use TinyXML, so I've seen that screen before.

The main problem is that kind of reference doesn't work for me - I need examples. Which is why I'm asking for examples from here.
Quote:Original post by Jason Petrasko
This lists:
virtual const TiXmlText * 	ToText () const 	Cast to a more defined type. Will return null if not of the requested type.

How would I manipulate this const TiXmlText* to do stuff with it? I mean, I could figure out a way to send it to the Lua part of my program and manipulate there, but then TinyXML isn't doing its work. I found a function called < a href="http://www.grinninglizard.com/tinyxmldocs/classTiXmlText.html#a11">Accept under TiXmlText, which looks like what I want, but I don't know how to use it or what it exactly does.

Thanks,

adam_o
_______________________My computer stats:Xcode 3.1.2Mac OS 10.5.8---Visual C++ 2008 Express EditionWindows XP---NetBeansUbuntu 9.04---Help needed here!
Thanks to everybody for the help, but the team has decided to ditch XML all together and import using an easier medium.
_______________________My computer stats:Xcode 3.1.2Mac OS 10.5.8---Visual C++ 2008 Express EditionWindows XP---NetBeansUbuntu 9.04---Help needed here!

This topic is closed to new replies.

Advertisement