How do I get the data off of an XML document?
Hello,
I'm unsure (and somewhat confused) how to get the data off of an XML document. Here's a piece of the file I have:
<movement>
<speed>20</speed>
<acceleration>225</acceleration>
<deceleration>225</deceleration>
<roll_anim>27<roll_anim>
<roll_time>0</roll_time>
<jump>-20</jump>
<special_anim>15</special_anim>
<special_value>90</special_value>
</movement>
<battle>
<power>0</power>
<defence>0</defence>
<charge_time>0</charge_time>
<energy_use>0</energy_use>
</battle>
How would I take those various values in the XML document from the XML document? Also, is the method for getting the values different compared to the method for getting text? Note: I do have a single root element, but it is not shown in the section of the file I have shown in this post.
Thanks for your help,
Neonotso
Re: How do I get the data off of an XML document?
You need one single root tag (at least, you usually do - I'm not certain if either of the available XML extensions enforce this). After that, use an extension like EasyXML to explore the tree.
Re: How do I get the data off of an XML document?
Okay, I do have a single root tag. I just didn't show it in the portion of the file I showed in my previous post. I guess my main question is, How do I use EasyXML to get the data off of the file?
Re: How do I get the data off of an XML document?
To quote Wikipedi: "Each XML document has one, and exactly one single root element. This element is also known as the document element. It encloses all the other elements and is therefore the sole parent element to all the other elements."
So basically all of the elements need to be a child of the root element. For example HTML documents (web pages) all start with <HTML> and end with </HTML> because HTML is the root (top-level) element. The only data before the root element is metadata such as document type, XML version or character encoding.
Re: How do I get the data off of an XML document?
I do have a single root element, but I suppose I should have actually displayed it in the first post (I only displayed a section of the document) to avoid this confusion. Thanks, though.
Here's how the document would look with one character profile in it (with the password area modified as to not reveal the password):
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<profiles>
<player id="NV7">
<info>
<name>Neonotso</name>
<password>*******</password>
<rank>admin</rank>
</info>
<story>
<level>1</level>
<enemy_power>1</enemy_power>
<healthbar>1</healthbar>
<pieces>0</pieces>
</story>
<stats>
<movement>
<speed>20</speed>
<acceleration>225</acceleration>
<deceleration>225</deceleration>
<roll_anim>27<roll_anim>
<roll_time>0</roll_time>
<jump>-20</jump>
<special_anim>15</special_anim>
<special_value>90</special_value>
</movement>
<battle>
<power>0</power>
<defence>0</defence>
<charge_time>0</charge_time>
<energy_use>0</energy_use>
</battle>
<generic>
<experience>0</experience>
<level>1</level>
<version>4</version>
<type>energy</type>
</generic>
</stats>
<inventory>
<slot_1>8</slot>
<slot_2>47</slot>
<slot_3>12</slot>
<slot_4>34</slot>
<slot_5>59</slot>
<slot_6>20</slot>
<slot_7>83</slot>
</inventory>
</player>
</profiles>
The single root element is "profiles". I would like to know how to use EasyXML to get the various values and text from the document to MMF2.
Thanks,
Neonotso
Re: How do I get the data off of an XML document?
I'm pretty sure you would use the autoparse system... an example should have came with the extension.
Check out this thread: http://www.clickteam.com/epicenter/ubbthreads.php?ubb=showflat&Number=20033
The file here: http://www.clickteam.com/epicenter/ubbthreads.php?ubb=download&Number=516
Should answer your questions, I think ^^
Also, may I ask why you are wanting to use XML?
Re: How do I get the data off of an XML document?
If you are using XML you should at least use <slot id="5">59</slot> instead of <slot_5>59</slot>, or you might as well be using INI (which is easier).
Re: How do I get the data off of an XML document?
Hi all,
Thanks! I think you are right that I should be using INI files instead of XML. I was trying XML because of an answer to a question I asked on this thread: here. Someone suggested I use INI, but someone else suggested XML, so I wasn't sure what to do. I'm much better at using INI, and truly I don't think that I'll be getting a massive amount of users on the game I'm making, so yes, I think INI would be better. If ever I endeavor to use XML, though, I'll most probably benefit from the info that has been provided.
Thanks for all the help! :)
Neonotso
Re: How do I get the data off of an XML document?
I actually don't think you should use the INI object for this at all.
XML gives you greater freedom to group your data together, and as you are doing something a bit more complex then simple configuration settings I would recommend that you use XML.
There are two ways to use the EasyXML object. There is the AutoParse system and then there is the manual way.
For your case I would recommend using the manual way becuase you only want to load one player at a time more then likely.
Start at the root element, get the first child of the element (Or get a child from an index). This will give you your first player element.
At the player element getting the first child again will give you the Info element. Now you can get the first child and the next siblings to get the Name, Password, and Rank by using the Get Child Text expression.
So here is the order so far:
Root Element
First Child
Current Element: Player
Get string attribute "id" (Store the id of the player).
First Child
Current Element: Info
First Child
Current Element: Name
Get Child Text (Get the name of the player and store it)
Next Sibling
Current Element: Password
Get Child Text (Get the password of the player and store it)
Next Sibling
Current Element: Rank
Get Child Text (Get the rank of the player and store it)
Parent
Next Sibling
Current Element: Story
First Child
Current Element: Level
And repeat ^^.
Re: How do I get the data off of an XML document?
Ahh... That makes sense. Cool, so now I can try out XML! Thanks very much! This sure is a great forum - I always get my questions answered! :)