| View previous topic :: View next topic |
MatOfLink

Joined: 22 Jun 2009 Posts: 31 Location: Montpellier , FRANCE
|
Posted: Mon Jun 22, 2009 12:45 pm Post subject: Loading an XML file |
|
|
|
Hi everyone,
I've got an XML file, and I haven't found how to load and use it.
Is it even possible ?
I'm looking for any tutorial, or a nice person who would like to help me
Thank you.
|
|
| Back to top |
|
|
perlohmann
Joined: 12 Feb 2009 Posts: 131
|
|
| Back to top |
|
|
MatOfLink

Joined: 22 Jun 2009 Posts: 31 Location: Montpellier , FRANCE
|
Posted: Mon Jun 22, 2009 4:40 pm Post subject: |
|
|
|
Mhmh thank you , already seen that. But actually I'd like to know I to use it(quick example would be great ). And I forgot to say that I need to do it with Javascript.. sorry
|
|
| Back to top |
|
|
HiggyB Unity Product Evangelist

Joined: 08 Dec 2006 Posts: 6152 Location: San Francisco CA USA
|
Posted: Mon Jun 22, 2009 11:49 pm Post subject: |
|
|
|
You can load the file either via .NET FileIO routines (locally stored file being loaded into a stand-alone executable) or by using the WWW class (remotely stored file being loaded into a stand-alone executable or a web player). Once you've loaded it you can either (a) use the .NET XML libraries as described above*, or (b) write something lean and simple yourself.
*The "problem" with the .NET libraries is that they are bloated and quite often far more than you really need. For example, I did a simple demo recently where I needed to load some very simple XML data, the _1MB_ .NET library felt dumb, so I rolled my own brief script to suit my needs.
If you want to see that script then let me know and I'll share it with you. It's not perfect, it's not ready for use in any random situation, but it is a proof of concept that rolling your own simple parser isn't that tough.
_________________ Tom Higgins - Product Evangelist at Unity Technologies ApS
http://unity3d.com | http://blogs.unity3d.com
Want to discuss my avatar? Do it here.
UNITE 2010, Save the Date: Nov. 10-12 in Montreal! |
|
| Back to top |
|
|
MatOfLink

Joined: 22 Jun 2009 Posts: 31 Location: Montpellier , FRANCE
|
Posted: Tue Jun 23, 2009 8:23 am Post subject: |
|
|
|
Yes I'm very interested in your script , it will allows me to start something I hope
My XML file is very simple , it describes the location of objects in the 3D scene. So once I i've accessed to it , it will be fine.
Thank you.
|
|
| Back to top |
|
|
HiggyB Unity Product Evangelist

Joined: 08 Dec 2006 Posts: 6152 Location: San Francisco CA USA
|
Posted: Tue Jun 23, 2009 11:57 pm Post subject: |
|
|
|
Ok, here ya go...
For reference, I'll again point the XML that I'm using, it's found here.
I first load the external XML file using the WWW class, then this is all that's needed:
| Code: | var tXMLParser : XMLParser = new XMLParser();
var tSimulationData : Hashtable[] = tXMLParser.ParseString(tWWW.data); |
I instantiate the parser then have it parse the provided string (again, retrieved via the WWW class). It produces a built-in array of hashtables whose values I can then look up. Each entry in the array represents a tag block in the XML, the named hashtable entries are the attributes of that tag block. So for example, based on my sample XML and parsing it with my scripts:
| Code: | var tXMLParser : XMLParser = new XMLParser();
var tSimulationData : Hashtable[] = tXMLParser.ParseString(tWWW.data);
Debug.Log(tSimulationData[1]["mass"]);
|
The above would produce "1.9891e30.0" (the mass of the Sun in kilograms ) as that's the second tag in the XML, and in specific the value for the attribute named "mass".
Disclaimer: this XML parser is not robust nor intended for all situations. Let it serve as an example of how you can build your own specific parser to meet your project's needs.
See the attached ZIP and have fun.
| Description: |
| Example XML parsing script. |
|
 Download |
| Filename: |
XML.zip |
| Filesize: |
2.58 KB |
| Downloaded: |
1212 Time(s) |
_________________ Tom Higgins - Product Evangelist at Unity Technologies ApS
http://unity3d.com | http://blogs.unity3d.com
Want to discuss my avatar? Do it here.
UNITE 2010, Save the Date: Nov. 10-12 in Montreal! |
|
| Back to top |
|
|
MatOfLink

Joined: 22 Jun 2009 Posts: 31 Location: Montpellier , FRANCE
|
Posted: Wed Jun 24, 2009 8:42 am Post subject: |
|
|
|
Thank you again
I'll try this and tell you the result.
Edit : All right , so your code helped me a little for what I want to do next ; but what I really need is actually how do I load the XML using the WWW class.
this: | HiggyB wrote: | | I first load the external XML file using the WWW class |
Then i'll know does this "tWWW" comes from.
Your help is very appriciated , I think i'm close to my goal thanks to you
|
|
| Back to top |
|
|
HiggyB Unity Product Evangelist

Joined: 08 Dec 2006 Posts: 6152 Location: San Francisco CA USA
|
Posted: Wed Jun 24, 2009 4:04 pm Post subject: |
|
|
|
That's super simple:
| Code: | var tWWW = new WWW("http://files.unity3d.com/tom/orbitsim/XML/SolarSystem.xml");
yield tWWW;
if (tWWW.error != null) {
// error, do something
} else {
// use the code I provided here :)
} |
You can't do the above in Update or Start though as it uses yield (coroutine), so call a helper function from Start/Update/etc. that contains your fetch-and-parse code.
_________________ Tom Higgins - Product Evangelist at Unity Technologies ApS
http://unity3d.com | http://blogs.unity3d.com
Want to discuss my avatar? Do it here.
UNITE 2010, Save the Date: Nov. 10-12 in Montreal! |
|
| Back to top |
|
|
MatOfLink

Joined: 22 Jun 2009 Posts: 31 Location: Montpellier , FRANCE
|
Posted: Thu Jun 25, 2009 8:30 am Post subject: |
|
|
|
Great !
Thank you I think i've everything I needed.
I didn't know I could use it likte that : new WWW().
|
|
| Back to top |
|
|
JenniC
Joined: 08 Jul 2009 Posts: 2
|
Posted: Wed Jul 08, 2009 6:01 pm Post subject: XML parsing |
|
|
|
For XML parsing, check out biterscripting at http://www.biterscripting.com . I use it to parse html - I have seen several biterscripts posted on the web that parse XML.
J
|
|
| Back to top |
|
|
MatOfLink

Joined: 22 Jun 2009 Posts: 31 Location: Montpellier , FRANCE
|
Posted: Wed Jul 08, 2009 7:17 pm Post subject: |
|
|
|
Thanks
|
|
| Back to top |
|
|
DASARADHI
Joined: 02 Jul 2009 Posts: 48
|
Posted: Tue Aug 18, 2009 6:10 am Post subject: XML file Architecture. |
|
|
|
Can you please tell me the xml architecture of your following file
http://files.unity3d.com/tom/orbitsim/XML/SolarSystem.xml
We are trying to open that but due to insufficient access, it is not opening.
Thanks.
_________________ DaSaRaDHI BENDI
Sr.Game Developer |
|
| Back to top |
|
|
HiggyB Unity Product Evangelist

Joined: 08 Dec 2006 Posts: 6152 Location: San Francisco CA USA
|
Posted: Tue Aug 18, 2009 5:26 pm Post subject: Re: XML file Architecture. |
|
|
|
Huh? You can't even open it? Enter it into a browser if necessary as it's not a protected file nor under any security limitations. But in any case, the file's XML contents are:
| Code: | <system camdistance="3.0" name="Inner-Planets">
<body name="Sun" mass="1.9891e30.0" radius="0.2" inclination="0.0" color="yellow" semimajoraxis="0.0" avgspeed="0.0"/>
<body name="Mercury" mass="3.3022e23.0" radius="0.04" inclination="6.34" color="tan" semimajoraxis="5.7909e10.0" avgspeed="4.787e4.0"/>
<body name="Venus" mass="4.8685e24.0" radius="0.12" inclination="2.19" color="orange" semimajoraxis="1.0821e11.0" avgspeed="3.502e4.0"/>
<body name="Earth" mass="5.9736e24.0" radius="0.12" inclination="1.5" color="blue" semimajoraxis="1.4960e11.0" avgspeed="2.9783e4.0"/>
<body name="Mars" mass="6.4185e23.0" radius="0.08" inclination="1.67" color="red" semimajoraxis="2.2794e11.0" avgspeed="2.4077e4.0"/>
</system> |
_________________ Tom Higgins - Product Evangelist at Unity Technologies ApS
http://unity3d.com | http://blogs.unity3d.com
Want to discuss my avatar? Do it here.
UNITE 2010, Save the Date: Nov. 10-12 in Montreal! |
|
| Back to top |
|
|
Spookytooth
Joined: 27 Jun 2009 Posts: 34 Location: North Carolina
|
Posted: Wed Sep 09, 2009 4:06 am Post subject: |
|
|
|
I can get your code snippets to work perfectly together. However, if I change the url to another xml link, it breaks. Is there something I should do to the parser?
Here's the link I'm trying to pull down: http://www.blindsquirreldigital.com/rss/jun.xml
I've also been trying to get a C# solution to work, but I don't know alot about C#.
| Code: | using UnityEngine;
using System.Collections;
using System.Xml;
using System;
public class getWeather3 : MonoBehaviour {
// Use this for initialization
void Start () {
StartCoroutine(LoadConfig());
}
public enum LoadConfig() {
string url = "http://www.blindsquirreldigital.com/rss/jun.xml";
WWW www = new WWW(url);
yield return www;
if (www.error == null)
{
//no error occured
string xml = www.data;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
}
else
{
Debug.Log("ERROR: " + error);
}
www.Dispose();
www = null;
}
} |
|
|
| Back to top |
|
|
HiggyB Unity Product Evangelist

Joined: 08 Dec 2006 Posts: 6152 Location: San Francisco CA USA
|
Posted: Wed Sep 09, 2009 4:30 pm Post subject: |
|
|
|
| Spookytooth wrote: | | I can get your code snippets to work perfectly together. However, if I change the url to another xml link, it breaks. Is there something I should do to the parser? |
My example XML parser isn't provided as a robust "it parses all" piece of code. As stated above:
| HiggyB wrote: | | Disclaimer: this XML parser is not robust nor intended for all situations. Let it serve as an example of how you can build your own specific parser to meet your project's needs. |
So yes, you might need to change/update my parser code depending on exactly where it's failing in your case.
_________________ Tom Higgins - Product Evangelist at Unity Technologies ApS
http://unity3d.com | http://blogs.unity3d.com
Want to discuss my avatar? Do it here.
UNITE 2010, Save the Date: Nov. 10-12 in Montreal! |
|
| Back to top |
|
|