Search Unity

Loading an XML file

Discussion in 'Scripting' started by MatOfLink, Jun 22, 2009.

  1. MatOfLink

    MatOfLink

    Joined:
    Jun 22, 2009
    Posts:
    31
    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.
     
  2. perlohmann

    perlohmann

    Joined:
    Feb 12, 2009
    Posts:
    221
  3. MatOfLink

    MatOfLink

    Joined:
    Jun 22, 2009
    Posts:
    31
    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 :roll:
     
  4. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    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.
     
  5. MatOfLink

    MatOfLink

    Joined:
    Jun 22, 2009
    Posts:
    31
    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.
     
  6. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    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 (csharp):
    1. var tXMLParser : XMLParser = new XMLParser();
    2. 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 (csharp):
    1. var tXMLParser : XMLParser = new XMLParser();
    2. var tSimulationData : Hashtable[] = tXMLParser.ParseString(tWWW.data);
    3. Debug.Log(tSimulationData[1]["mass"]);
    4.  
    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.
     

    Attached Files:

  7. MatOfLink

    MatOfLink

    Joined:
    Jun 22, 2009
    Posts:
    31
    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:
    ;)
    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 :)
     
  8. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    That's super simple:

    Code (csharp):
    1. var tWWW = new WWW("http://files.unity3d.com/tom/orbitsim/XML/SolarSystem.xml");
    2. yield tWWW;
    3. if (tWWW.error != null) {
    4.   // error, do something
    5. } else {
    6.   // use the code I provided here :)
    7. }

    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.
     
  9. MatOfLink

    MatOfLink

    Joined:
    Jun 22, 2009
    Posts:
    31
    Great !

    Thank you I think i've everything I needed.
    I didn't know I could use it likte that : new WWW().
     
  10. JenniC

    JenniC

    Joined:
    Jul 8, 2009
    Posts:
    2
    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
     
  11. MatOfLink

    MatOfLink

    Joined:
    Jun 22, 2009
    Posts:
    31
  12. DASARADHI

    DASARADHI

    Joined:
    Jul 2, 2009
    Posts:
    65
  13. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    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 (csharp):
    1. <system camdistance="3.0" name="Inner-Planets">
    2. <body name="Sun" mass="1.9891e30.0" radius="0.2" inclination="0.0" color="yellow" semimajoraxis="0.0" avgspeed="0.0"/>
    3. <body name="Mercury" mass="3.3022e23.0" radius="0.04" inclination="6.34" color="tan" semimajoraxis="5.7909e10.0" avgspeed="4.787e4.0"/>
    4. <body name="Venus" mass="4.8685e24.0" radius="0.12" inclination="2.19" color="orange" semimajoraxis="1.0821e11.0" avgspeed="3.502e4.0"/>
    5. <body name="Earth" mass="5.9736e24.0" radius="0.12" inclination="1.5" color="blue" semimajoraxis="1.4960e11.0" avgspeed="2.9783e4.0"/>
    6. <body name="Mars" mass="6.4185e23.0" radius="0.08" inclination="1.67" color="red" semimajoraxis="2.2794e11.0" avgspeed="2.4077e4.0"/>
    7. </system>
     
  14. Spookytooth

    Spookytooth

    Joined:
    Jun 27, 2009
    Posts:
    57
    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 (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Xml;
    4. using System;
    5.  
    6. public class getWeather3 : MonoBehaviour {
    7.     // Use this for initialization
    8.     void Start () {
    9.         StartCoroutine(LoadConfig());
    10.     }  
    11.  
    12. public enum LoadConfig() {
    13.   string url = "http://www.blindsquirreldigital.com/rss/jun.xml";
    14.   WWW www = new WWW(url);
    15.   yield return www;
    16.   if ([url]www.error[/url] == null)
    17.   {
    18.     //no error occured
    19.     string xml = [url]www.data;[/url]
    20.     XmlDocument xmlDoc = new XmlDocument();
    21.     xmlDoc.LoadXml(xml);    
    22.   }
    23.   else
    24.   {
    25.     Debug.Log("ERROR: " + error);
    26.   }
    27.   www.Dispose();
    28.   www = null;
    29. }
    30. }
     
  15. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    My example XML parser isn't provided as a robust "it parses all" piece of code. As stated above:

    So yes, you might need to change/update my parser code depending on exactly where it's failing in your case.
     
  16. spacefrog

    spacefrog

    Joined:
    Jun 14, 2009
    Posts:
    734
    Just a remark:

    Just converted the XMLParser to C# - if anybody is interested i could post the files. I did'nt test thoroughly (actually only a simple HiggyB - standard - "Sun-Mass" - Test like in the example above...) and it worked on the first try ....
    But it may contain pitfalls due to the ArrayList conversion. The code itself (js and C# ) would qualify for a place on the Wiki - wouldn't it ?
     
  17. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    It would be a great addition to the wiki, but perhaps you should test it thoroughly first - you might cause yourself some embarassment if you publish code that doesn't work!
     
  18. Flimgoblin

    Flimgoblin

    Joined:
    Nov 25, 2009
    Posts:
    89
    I've created a generic XML parser (in pure UnityScript - no libraries required) that in theory should work with any XML input. No validation or XML file generation and I expect it'll break in interesting ways if you give it bad input.

    It's also not been tested to death by any means (though it seems to behave well enough on an RSS feed I fed it).

    http://www.roguishness.com/unity/

    Give it a go and let me know how you get on.
    Any comments, bug reports, etc. are more than welcome.
     
  19. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    8kb JavaScript file for use in Unity, well done! I didn't test it so much as only downloaded it to check out the file size, if it works and it's that small then you sir, are awesome! :)


    Thanks for sharing!
     
  20. Flimgoblin

    Flimgoblin

    Joined:
    Nov 25, 2009
    Posts:
    89
    No problem :) figure I've had enough for free from the community tutorials (never mind Unity itself) that I should give a little bit back.

    It'll get a bit more of a thorough testing in the next few weeks (hopefully) when I put it into use on a project but a quick check over this evening has found one bug already - it was including the tailing
    Code (csharp):
    1. >
    from singleton tags (e.g.
    Code (csharp):
    1. <foo/>
    ) as if it was part of the text of the containing element.

    v0.2 uploaded with a fix.

    It really is just a "turn XML file into object" script though, hence small size - none of the validation or construction or node traversing stuff you get from a proper XML library. That said if all you're doing is hammering nails, you don't really need much more than a hammer.
     
  21. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
  22. DIProgan

    DIProgan

    Joined:
    Dec 21, 2009
    Posts:
    16
    I have a very simple XML file imported as asset looking like this:
    <1>yada yada</1><2>more yada</2><3> etc.</3>

    Now by using Random.Range in my Javascript file I want the representative string read into a variable in that js. Code please?

    This will be a very large xml file with 1000+ entries. Hopefully it wont cause any problems.
     
  23. DIProgan

    DIProgan

    Joined:
    Dec 21, 2009
    Posts:
    16
    Well figured out how to load an external xml file now via iostream, maybe that does nothing towards the xml part of it but oh well... Just need to figure out the easiest way to parse it now..
     
  24. Flimgoblin

    Flimgoblin

    Joined:
    Nov 25, 2009
    Posts:
    89
    If you're writing for a desktop PC/Mac or are already using the System library dll (not sure what pulls that in tbh) then you can go with System.XML as described above.

    If you want something lighter for web or iphone you could try this lightweight parser I've written.

    (using System.XML adds 1M to your final build, which doesn't really matter if tis a 500M build already, bit of a pain if you're under 500k for the rest of it)
     
  25. Flimgoblin

    Flimgoblin

    Joined:
    Nov 25, 2009
    Posts:
    89
    Actually, looking at your data file if it's that simple you're better off not bothering with a parser and just splitting the string.

    And if it's just a list of values in numerical order - don't use XML, put the values in a file and use a newline (\n) to separate them*. Pull the file into Unity and split on newline to make an array of the values.

    If you only really need one value at a time and you can use fixed width values you might be able to seek within the stream and just grab the value you need - not really played around with that in Unity, and probably not worth the hassle unless you have a really big file, but in theory it'll be faster than loading the whole file in each time you use it.


    *unless you have newlines in your data, in which case use something else, and if you have arbitrary data you'll have to encode it in the file or use some sort of CSV formatting but then you need a parser...
     
  26. DIProgan

    DIProgan

    Joined:
    Dec 21, 2009
    Posts:
    16
    I gave up on XML it was just way more of a hassle than expected. Wanted to learn it but not this time. Parsing the old fashion way now IndexOf SubString from a xml compatible file. Works. Will have to see what happends when the filesize goes up (woe me :p).
     
  27. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    FWIW, I think it's very bad form to come on here, cite what you want and simply ask "code please?". You must demonstrate some attempt on your own for many of us won't simply hand you results for free, you must display efforts to try and figure things out on your own first. If you clearly show an ounce of effort I'll provide a pound in return, you show nothing and I'll likely offer nothing...

    FWIW, my sample code used in my ObitSim demo should have been relatively easy to convert over if you ever want to go back to XML in particular.
     
  28. craigerz

    craigerz

    Joined:
    Mar 30, 2009
    Posts:
    19
    Higgy,

    Was originally looking for help with parsing multiple variables from mysql db, or even through php using www.data. Xml is my weak spot, but your code worked flawlessly.

    Thank you!!!
     
  29. chupacabras

    chupacabras

    Guest

    Joined:
    Jun 13, 2010
    Posts:
    7
    Thanks for XML files HiggyB ... I will try to use for solve my problem =D
     
  30. AlexG

    AlexG

    Joined:
    Jul 7, 2010
    Posts:
    45
    Hey, I just started using unity and it's come to the point where I've had to learn XML format and XML parsing.

    Let me first say that this is an amazing thread. I was parsing XML within 30 minutes.

    Using the code/insight that you've (HiggyB) provided I made something like this:
    Code (csharp):
    1. function Start()
    2. {
    3.     Helper();
    4. }
    5.  
    6. function Helper ()
    7. {
    8.     var tWWW = new WWW("http://files.unity3d.com/tom/orbitsim/XML/SolarSystem.xml");
    9.     yield tWWW;
    10.     if (tWWW.error != null)
    11.     {
    12.        // error, do something
    13.     }
    14.         else
    15.     {
    16.         var tXMLParser : XMLParser = new XMLParser();
    17.         var tSimulationData : Hashtable[] = tXMLParser.ParseString(tWWW.data);
    18.         Debug.Log(tSimulationData[1]["mass"] + " " + tSimulationData[2]["mass"] + " " + tSimulationData[3]["mass"]);
    19.     }
    20. }
    Here's my question:

    How can I modify this so I can load XML files locally and not using the WWW constructor

    Code (csharp):
    1. var tWWW = new WWW("http://files.unity3d.com/tom/orbitsim/XML/SolarSystem.xml");
    Is there a data type and constructor that can do something like this:

    Code (csharp):
    1. var magicVariable = new magicConstructor("/Assets/XMLFiles/myXMLfile.xml");
    2. magicVariable.
    And then use that variable like this:

    Code (csharp):
    1. var tSimulationData : Hashtable[] = tXMLParser.ParseString(magicVariable.data);
    Or better yet, is there some way I can link the XML file directly from the project folder to the inspector. For example:

    Code (csharp):
    1. var someObject : GameObject;
    I can then attach this script to some game object in the hierarchy and drag and drop a game object from the project folder into the someObject variable slot in the inspector.

    Using this same concept, can I do something like this:

    Code (csharp):
    1. var someXMLFile : XMLFile;
    Catch my drift? I just wanna load XML files locally. I would imagine this is possible, I just don't know what data types and functions I'm supposed to be using.

    EDIT:

    NEVERMIND!!! I figured it out :D. Unity is soooooo simple I love it!

    Code (csharp):
    1. var test : TextAsset;
    2.  
    3. function Start()
    4. {
    5.     Helper();
    6. }
    7.  
    8. function Helper()
    9. {
    10.     var tXMLParser : XMLParser = new XMLParser();
    11.     var tData : Hashtable[] = tXMLParser.ParseString(test.text);
    12.     Debug.Log(tData[1]["mass"]);
    13. }
    14.  
    Really cool! Now it's time to learn about how you wrote the parser! I don't like using other people's code without understanding it! Thanks for this thread guys :D.

    Alex
     
  31. Millertaker

    Millertaker

    Joined:
    Sep 20, 2010
    Posts:
    3
    when i try to execute the C# code the IDE trhows me this error:

    Assets/Standard Assets/Scripts/LoadXML.cs(11,31): error CS8025: Parsing error

    Someone knows what could be?
    Thanks
     
  32. Millertaker

    Millertaker

    Joined:
    Sep 20, 2010
    Posts:
    3
    when i try to execute the C# code the IDE trhows me this error:

    Assets/Standard Assets/Scripts/LoadXML.cs(11,31): error CS8025: Parsing error

    Someone knows what could be?
    Thanks
     
  33. chupacabras

    chupacabras

    Guest

    Joined:
    Jun 13, 2010
    Posts:
    7
    I think the problem is the XML format ...

    Could you post the code ?
     
  34. KennyW

    KennyW

    Joined:
    Aug 20, 2010
    Posts:
    128
    I can read an XML file from my data folder without problem. But my question here is how will I be able to read an XML file from inside my Unity3D resources (i.e. in the "project" tab or project "Hierarchy" instead of a file path?
     
  35. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    you can't

    if you drop it into the project it will cease to be an xml (it will no longer exist as file in the build but will be part of the sharedassets.dll file), it will become a text asset where you can get the string from but only that.
     
  36. KennyW

    KennyW

    Joined:
    Aug 20, 2010
    Posts:
    128
    Ok. Thank so much again, dreamora. :D
     
  37. Millertaker

    Millertaker

    Joined:
    Sep 20, 2010
    Posts:
    3
    Was a error metod implementation of the corrutine

    in the example it's public num
    to solve this i used public IEnumerator
     
  38. Ardan

    Ardan

    Joined:
    Oct 5, 2009
    Posts:
    68
    In the XMLParser.js that HiggyB uploaded i get this error:
    Code (csharp):
    1. Assets/Scripts/XML/XMLParser.js(59,45): BCE0022: Cannot convert 'System.Array' to 'System.Collection.Hashtable[]'.
    2.  
    at line 53 with this code:
    Code (csharp):
    1. return tSystemData.ToBuiltin(Hashtable);
    2.  
    I can't figure out how to fix it. And i would really appreciate your help.
     
  39. esvaldo

    esvaldo

    Joined:
    Dec 11, 2009
    Posts:
    52
    I get the same error. Maybe it doesn't play well with 3.1
     
  40. tikod

    tikod

    Joined:
    Nov 30, 2010
    Posts:
    19
    Same problem for me with unity 3.1...someone know another script to parse xml file ?
     
  41. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Welcome to the forum, tikod!

    You can fix the existing script by changing the line with the error to this:-
    Code (csharp):
    1. return tSystemData.ToBuiltin(Hashtable) as Hashtable[];
     
  42. cgoran

    cgoran

    Joined:
    Jun 11, 2005
    Posts:
    176
    I'm having issues running the .Count function.

    Running:
    Debug.Log(tHash.Count);

    Returns:
    BCE0019: 'Count' is not a member of '(System.Collections.Hashtable)'.

    Thanky!
     
  43. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    All this to scrape off 1mb from the final file size?
     
  44. deepak

    deepak

    Joined:
    Aug 10, 2010
    Posts:
    44
    Hi

    I am building an application where i want to load data thro XML in the assets folder. When i build an standalone application, I am not getting the xml in the build. Its working fine in Editor. Any Suggestions?
     
  45. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    it won't go through if you put it into the asset folder. those files used end in the assembly and would be used through textassets, those not used never get into the build.

    you could use the Resources folder, or copy it along manually on builds while keeping it outsides the assets folder
     
  46. deepak

    deepak

    Joined:
    Aug 10, 2010
    Posts:
    44
    Thanks dreamora
     
  47. Lordships

    Lordships

    Joined:
    Feb 14, 2011
    Posts:
    1
    I am attempting to utilize this XML Parser.

    I have a script to instantiate the XML Parser:

    Code (csharp):
    1. var tWWW = new WWW("XML/xmlDataFiles.xml");
    2. yield tWWW;
    3. if (tWWW.error != null) {
    4.   // error, do something
    5. } else {
    6.     var tXMLParser : XMLParser = new XMLParser();
    7.     var tSimulationData : Hashtable[] = tXMLParser.ParseString(tWWW.data);
    8. }
    I made the minor adjustment to the parser:

    Code (csharp):
    1. // Return the system data array as a builtin array
    2.         return tSystemData.ToBuiltin(Hashtable) as Hashtable[];
    3.  
    And as a result I am getting a very non descriptive compile time error:

    Any Suggestions? I am at a loss with this very useless error.
     
    Last edited: Feb 14, 2011
  48. chrismcowden

    chrismcowden

    Joined:
    Jun 23, 2009
    Posts:
    3
    I just did a quick conversion of Tom's XMLParser to C#. I thought I saw someone else mention they did but I didn't see it. Either way, here it is.

    *fair warning: this has no compilation warnings/errors in Unity, but I haven't run it because it is specific to Tom's XML schema. I repeat, this is untested (from the C# perspective.) I just wanted to give back to the Unity community that's given so much.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. class XMLParser
    6. {
    7.     XMLParser() { }
    8.  
    9.     // -----
    10.     // Public Functions
    11.     // -----
    12.    
    13.     Hashtable[] ParseString(string aXMLString)
    14.     {
    15.         // Initialize the system data array
    16.         ArrayList tSystemData = new ArrayList();
    17.        
    18.         // Look for and remove the XML header tag
    19.         aXMLString = RemoveXMLHeaderTag(aXMLString);
    20.        
    21.         // Pull the configuration data
    22.         Hashtable tConfigData = GetElementAttributes(aXMLString, "system");
    23.         tSystemData.Add(tConfigData);
    24.        
    25.         // Look for and remove the wrapping system tag
    26.         aXMLString = RemoveSystemTag(aXMLString);
    27.        
    28.         // Process all body tags
    29.         Hashtable tNextBody = GetElementAttributes(aXMLString, "body");
    30.         while (tNextBody != null) {
    31.             // Add the body to the system data array
    32.             tSystemData.Add(tNextBody);
    33.            
    34.             // Strip the current body tag
    35.             aXMLString = RemoveBodyTag(aXMLString);
    36.            
    37.             // Look for the next body tag
    38.             tNextBody = GetElementAttributes(aXMLString, "body");
    39.         }
    40.        
    41.         // Return the system data array as a builtin array
    42.         return tSystemData.ToArray(typeof(Hashtable)) as Hashtable[];
    43.     }
    44.    
    45.    
    46.     // -----
    47.     // Private Functions
    48.     // -----
    49.    
    50.     private Hashtable GetElementAttributes(string aXMLString, string aElementName)
    51.     {
    52.         // Look for the next element with the provided name
    53.         int tElementStart = aXMLString.IndexOf("<" + aElementName + " ");
    54.         int tElementEnd = aXMLString.IndexOf(">");
    55.         if ((tElementStart > -1)  (tElementEnd > tElementStart)) {
    56.             // Initialize the attributes hashtable
    57.             Hashtable tAttributes = new Hashtable();
    58.            
    59.             // Pull the attribute string of the element tag
    60.             int tStripStart = tElementStart + 2 + aElementName.Length;
    61.             int tStripLength = tElementEnd - tStripStart;
    62.             string tAttributeString = aXMLString.Substring(tStripStart, tStripLength);
    63.            
    64.             // Strip any leading/trailing space characters
    65.             tAttributeString = tAttributeString.Trim();
    66.            
    67.             // Split into name/value chunks, and walk each chunk
    68.             string[] tChunks = tAttributeString.Split(" "[0]);
    69.             for (int i = 0; i < tChunks.Length; i++) {
    70.                 // Split the chunk into separate name and value strings
    71.                 string tChunkString = tChunks[i].ToString();
    72.                 if (tChunkString != "/") {
    73.                     string[] tSubStrings = tChunkString.Split("="[0]);
    74.                     string tNameString = tSubStrings[0].ToString();
    75.                     string tValueString = tSubStrings[1].ToString();
    76.                
    77.                     // Store the name/value (stripping the leading/trailing "s) pair information
    78.                     // in the attributes hashtable
    79.                     tAttributes[tNameString] = tValueString.Substring(1, (tValueString.Length - 2));
    80.                 }
    81.             }
    82.            
    83.             // Return the attributes hashtable
    84.             return tAttributes;
    85.         } else {
    86.  
    87.             // Return null
    88.             return null;
    89.         }
    90.     }
    91.    
    92.     private string RemoveBodyTag(string aXMLString)
    93.     {
    94.         // Look for a body tag
    95.         int tBodyStart = aXMLString.IndexOf("<body ");
    96.         int tBodyEnd = aXMLString.IndexOf("/>");
    97.         if ((tBodyStart > -1)  (tBodyEnd > tBodyStart)) {
    98.             // Strip the body tag from the string
    99.             int tStripStart = tBodyEnd + 2;
    100.             aXMLString = aXMLString.Substring(tStripStart);
    101.         }
    102.        
    103.         // Return the trimmed XML string
    104.         return aXMLString;
    105.        
    106.     }
    107.    
    108.     private string RemoveSystemTag(string aXMLString)
    109.     {
    110.         // Look for the system tag
    111.         int tSystemStart = aXMLString.IndexOf(">");
    112.         int tSystemEnd = aXMLString.IndexOf("</system>");
    113.         if ((tSystemStart > -1)  (tSystemEnd > tSystemStart)) {
    114.             // Strip the system start/end elements
    115.             int tStripStart = tSystemStart + 1;
    116.             int tStripLength = tSystemEnd - tStripStart;
    117.             aXMLString = aXMLString.Substring(tStripStart, tStripLength);
    118.         }
    119.  
    120.         // Return the trimmed XML string
    121.         return aXMLString;
    122.     }
    123.    
    124.     private string RemoveXMLHeaderTag(string aXMLString)
    125.     {
    126.         // Look for the XML header tag
    127.         int tHeaderStart = aXMLString.IndexOf("<?xml");
    128.         int tHeaderEnd = aXMLString.IndexOf("?>");
    129.         if ((tHeaderStart > -1)  (tHeaderEnd > tHeaderStart)) {
    130.             // Strip the XML header tag from the string
    131.             int tStripStart = tHeaderEnd + 2;
    132.             aXMLString = aXMLString.Substring(tStripStart);
    133.         }
    134.        
    135.         // Return the trimmed XML string
    136.         return aXMLString;
    137.     }
    138. }
    139.  
    140. internal class XMLTag
    141. {
    142.    public ArrayList Children = new ArrayList();
    143.    public string Name;
    144.    public Object Value;
    145.    
    146.    XMLTag(string aName, Object aValue)
    147.    {
    148.       Name = aName;
    149.       Value = aValue;
    150.    }
    151.  
    152. }
    153.  
     
  49. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    Is there any way to generate an xml file from Unity. Just want to log some x,y,z positions and then plot them?
     
  50. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    For starters

    For later