Loading an XML file
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Unity Community Index // Scripting
View previous topic :: View next topic  
Author Message
MatOfLink



Joined: 22 Jun 2009
Posts: 31
Location: Montpellier , FRANCE

PostPosted: Mon Jun 22, 2009 12:45 pm    Post subject: Loading an XML file Reply with quote
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 Wink

Thank you.
Back to top
View user's profile Send private message Visit poster's website
perlohmann



Joined: 12 Feb 2009
Posts: 131

PostPosted: Mon Jun 22, 2009 4:11 pm    Post subject: Reply with quote
Can recommend looking at XmlDocument and XmlNode.

http://msdn.microsoft.com/en-us/library/system.xml.xmldocument_members.aspx

http://msdn.microsoft.com/en-us/library/system.xml.xmlnode_members.aspx


//perlohmann

_________________
Home sweet home
Back to top
View user's profile Send private message
MatOfLink



Joined: 22 Jun 2009
Posts: 31
Location: Montpellier , FRANCE

PostPosted: Mon Jun 22, 2009 4:40 pm    Post subject: Reply with quote
Mhmh thank you , already seen that. But actually I'd like to know I to use it(quick example would be great Wink ). And I forgot to say that I need to do it with Javascript.. sorry Rolling Eyes
Back to top
View user's profile Send private message Visit poster's website
HiggyB
Unity Product Evangelist


Joined: 08 Dec 2006
Posts: 6152
Location: San Francisco CA USA

PostPosted: Mon Jun 22, 2009 11:49 pm    Post subject: Reply with quote
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
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger
MatOfLink



Joined: 22 Jun 2009
Posts: 31
Location: Montpellier , FRANCE

PostPosted: Tue Jun 23, 2009 8:23 am    Post subject: Reply with quote
Yes I'm very interested in your script , it will allows me to start something I hope Wink
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
View user's profile Send private message Visit poster's website
HiggyB
Unity Product Evangelist


Joined: 08 Dec 2006
Posts: 6152
Location: San Francisco CA USA

PostPosted: Tue Jun 23, 2009 11:57 pm    Post subject: Reply with quote
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 Smile ) 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.



XML.zip
 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
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger
MatOfLink



Joined: 22 Jun 2009
Posts: 31
Location: Montpellier , FRANCE

PostPosted: Wed Jun 24, 2009 8:42 am    Post subject: Reply with quote
Thank you again Wink
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
Wink
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 Smile
Back to top
View user's profile Send private message Visit poster's website
HiggyB
Unity Product Evangelist


Joined: 08 Dec 2006
Posts: 6152
Location: San Francisco CA USA

PostPosted: Wed Jun 24, 2009 4:04 pm    Post subject: Reply with quote
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
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger
MatOfLink



Joined: 22 Jun 2009
Posts: 31
Location: Montpellier , FRANCE

PostPosted: Thu Jun 25, 2009 8:30 am    Post subject: Reply with quote
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
View user's profile Send private message Visit poster's website
JenniC



Joined: 08 Jul 2009
Posts: 2

PostPosted: Wed Jul 08, 2009 6:01 pm    Post subject: XML parsing Reply with quote
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
View user's profile Send private message
MatOfLink



Joined: 22 Jun 2009
Posts: 31
Location: Montpellier , FRANCE

PostPosted: Wed Jul 08, 2009 7:17 pm    Post subject: Reply with quote
Thanks Wink
Back to top
View user's profile Send private message Visit poster's website
DASARADHI



Joined: 02 Jul 2009
Posts: 48

PostPosted: Tue Aug 18, 2009 6:10 am    Post subject: XML file Architecture. Reply with quote
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
View user's profile Send private message
HiggyB
Unity Product Evangelist


Joined: 08 Dec 2006
Posts: 6152
Location: San Francisco CA USA

PostPosted: Tue Aug 18, 2009 5:26 pm    Post subject: Re: XML file Architecture. Reply with quote
DASARADHI wrote:
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.

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
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger
Spookytooth



Joined: 27 Jun 2009
Posts: 34
Location: North Carolina

PostPosted: Wed Sep 09, 2009 4:06 am    Post subject: Reply with quote
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
View user's profile Send private message
HiggyB
Unity Product Evangelist


Joined: 08 Dec 2006
Posts: 6152
Location: San Francisco CA USA

PostPosted: Wed Sep 09, 2009 4:30 pm    Post subject: Reply with quote
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
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger
Post new topic   Reply to topic    Unity Community Index // Scripting All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum