SimpleXML - Lightweight XML importer / exporter

XML is a handy format for communication between your game and a webserver.

I needed a lightweight importer / exporter, that uses only 1 line of code to activate and produces a plain and simple structure of Hashtables and ArrayLists. And I wanted it to be a bit forgiving in case of XML format errors as well. I hate it when an importer throws an exception the very second something is not 100% according to standards.

So I made the importer and added some handy functions to the existing Hashtable and ArrayList classes, to easily retrieve information.

Import a JSON string:
myHashtable = SimpleXmlImporter(xmlString);

Import only the tag named “materials”

materialProperties = SimpleXmlImporter.Import(xmlString, “materials”);

Export a JSON string:
string xmlString = myHashtable.XmlString();

Query the hierarchy
goodBook = myHashtable.GetNodeWithProperty(“Author”, “George Orwell”);

All source code is included and written in C#. There are no DLL’s or hidden stuff.

View the online webplayer demo (also included in the package) here
The documentation can be viewed on our website.

Hi
I am trying to export to an XML file with a list of objects which are in the scene.
After I will have this file loaded into the webplayer page(with some xml to html tool) so that I can create some weblinks to the objects in the scene.
I saw the demo script but it looks a bit to complicate for me, with so many extra functions.
I just need to export the list by name or id
could you tell me how to do it?
Many thanks
Nic

You need to build a hierarchy out of ArrayList and Hashtabe objects that you fill with the information you want to export. Then you call myHashtable.XmlString() and get the XML as a string. Or you call myArrayList.XmlString()

HI there!
I have this as my last script and I am able to change material on one object by assigning the material of another object
The concept is to create cubes objects in the scene that function as containers for materials. Similar when painting we pick colors from buckets. The xml file does the work of assigning materials to a certain objects. However this works just for one object…how can I implemet it so that I don’t have to hard coding for each single object?
Thanks Nic

private IEnumerator SetupTown(Hashtable townDefinition) {
    ArrayList elementDefinitions = townDefinition.GetArrayList("element");
    for(int i=0;i<elementDefinitions.Count;i++) {
        Hashtable elementDefinition = elementDefinitions.GetHashtable(i);
        Debug.Log("building element:" + elementDefinition.JsonString());
       
        int objID = elementDefinition.GetInt("object");
        Debug.Log("Found object named:"+ myobjects[objID] +" with ID number:"+ objID);
           
        int matID = elementDefinition.GetInt("material");           
        Debug.Log("Found material named: "+ mymaterials[matID] +" with ID number:"+ matID);

        myobjects[objID].renderer.material = mymaterials[matID].renderer.material;
    }       
yield return elementDefinitions;
}

and the xml file:

<element object="0"  material="0" > roof </element>
<element object="1"  material="1" > wall </element>

Your xml should contains a list of elements:

<elements>
    <element object="0"  material="0" > roof </element>
    <element object="1"  material="1" > wall </element>
</elements>

Your code should request the list first and then iterate through it:

private IEnumerator SetupTown(Hashtable townDefinition) {
    ArrayList elements = townDefinition.GetArrayList("elements");
    for(int i=0;i<elements.Count;i++) {
        Hashtable element = elements.GetHashtable(i);
        // ... do your stuff here
    }
}

Hi
Thanks very much , I understand how it works now and works fine, I had this set up before but for some reasons I had taken out the elements tag.
Thanks for helping

Hi again
I am trying to do this in order to deactivate/activate objects in the array:

bool activate = elementDefinition.GetBool("active");
myobjects[objID].SetActive(activate);

then the xml

<element  active="false"  object="0"  material="0" > roof </element>

but the object is always false
Is it possiblle to use bool here? Or it doesn’t work?
many thanks

@imagoculturae When you call “myHashtable.GetBool(someKey)” it checks whether the value is 1 or 0.
Version 2.0 will now also return true when the value equals “true”, “True”, or “TRUE”.

Other changes in version 2.0:

  • improved speed (approx. 2x faster)
  • better tolerance for XML formatting errors like omitting double quotes or forgetting a closing tag.

Version 2.0 has been submitted for approval and will be available in a few days.

Hi.
It seems to be in c#.
Can I use this in my javascript app?

Thanks.

Yes you can, but you need to put the C# scripts in the Plugins folder, so that they get compiled before the javascripts

execuse me, how can i use it ? just click the import after buying it ? there is an error says :
the referenced script on this behavior is missing !

Did you import everything? It sounds as if the DemoCtrl.cs script was not imported. The entire Demo Scene only consists of 1 gameobject, the “Main Camera”. And that gameobject has a component called “DemoCtrl”. So if Unity says the referenced script is missing and you are opening the Demo scene, it is very probable that you haven’t imported the DemoCtrl. You can fix the refence by dragging the script in the correct field again.

Thank you, everything is working fine now :slight_smile:

Hi,
Think this is the route I want to go for displaying descriptions about selected objects in my game. Is it possible to pass a ‘description’ over to a UI Text component?
Cheers,
Rusty

Hi Russel, are you sure you posted in the right thread? This one is about importing XML files with SimpleXML. If you have imported a description you can send it to aUI TextComponent with:
myTextComponent.text = someDescription;

Hey, Yeh I’m not entirely sure it’s what I want…But wanted to write some XML and then import it into unity to read when clicked on an object. But from what you’ve just said sounds like I don’t need the script to help with this! My bad.
Cheers

Hello, I want to import text to my game, so I just modify the xml on web server and on the game the UI text will change automatically… is that possible? I was looking the examples, and I saw how to import but How can I extract only the description text, because you can search by author and it will show all not only author. how could I extract only the text I want? sorry I new with all this… thanks!!!

Seems to have a problem somewhere. This xml file:

<?xml version="1.0" encoding="UTF-8"?>

Doesn’t get parsed correctly. The out3 node ends up inside the out1 node.

The fix i found for it was changing part of SimpleXmlImporter.SetPropertyValue
From:

if(parentFirstNode.ContainsKey(key) )

To:

if(parentFirstNode.ContainsKey(key) && !parentFirstNode.ContainsKey(“.tag.”))

Hi Orbcreation,
I try to load the town example xml file from webserver.
I uncommented the line
SetupTownFromServer(“http://beispiel.de/town.xml”); // from webserver
and commented the line
SetupTownFromFile();
and loaded the file to the mentioned location. But it doesent work.
It does`nt execute the SetupTownFromServer.
What am I missing?
Thanks a lot,
Stefan