Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

SimpleXML - Lightweight XML importer / exporter

Discussion in 'Assets and Asset Store' started by Orbcreation, Nov 26, 2014.

  1. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    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"<br>
    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.

     
  2. imagoculturae

    imagoculturae

    Joined:
    Apr 8, 2014
    Posts:
    81
    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
     
  3. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    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()
     
    Last edited: Mar 11, 2015
  4. imagoculturae

    imagoculturae

    Joined:
    Apr 8, 2014
    Posts:
    81
    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

    Code (CSharp):
    1.  
    2. private IEnumerator SetupTown(Hashtable townDefinition) {
    3.     ArrayList elementDefinitions = townDefinition.GetArrayList("element");
    4.     for(int i=0;i<elementDefinitions.Count;i++) {
    5.         Hashtable elementDefinition = elementDefinitions.GetHashtable(i);
    6.         Debug.Log("building element:" + elementDefinition.JsonString());
    7.        
    8.         int objID = elementDefinition.GetInt("object");
    9.         Debug.Log("Found object named:"+ myobjects[objID] +" with ID number:"+ objID);
    10.            
    11.         int matID = elementDefinition.GetInt("material");          
    12.         Debug.Log("Found material named: "+ mymaterials[matID] +" with ID number:"+ matID);
    13.  
    14.         myobjects[objID].renderer.material = mymaterials[matID].renderer.material;
    15.     }      
    16. yield return elementDefinitions;
    17. }
    and the xml file:
    Code (CSharp):
    1. <element object="0"  material="0" > roof </element>
    2. <element object="1"  material="1" > wall </element>        
     
  5. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    Your xml should contains a list of elements:
    Code (CSharp):
    1. <elements>
    2.     <element object="0"  material="0" > roof </element>
    3.     <element object="1"  material="1" > wall </element>
    4. </elements>
    Your code should request the list first and then iterate through it:
    Code (CSharp):
    1. private IEnumerator SetupTown(Hashtable townDefinition) {
    2.     ArrayList elements = townDefinition.GetArrayList("elements");
    3.     for(int i=0;i<elements.Count;i++) {
    4.         Hashtable element = elements.GetHashtable(i);
    5.         // ... do your stuff here
    6.     }
    7. }
    8.  
     
  6. imagoculturae

    imagoculturae

    Joined:
    Apr 8, 2014
    Posts:
    81
    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
     
  7. imagoculturae

    imagoculturae

    Joined:
    Apr 8, 2014
    Posts:
    81
    Hi again
    I am trying to do this in order to deactivate/activate objects in the array:
    Code (CSharp):
    1. bool activate = elementDefinition.GetBool("active");
    2. myobjects[objID].SetActive(activate);
    then the xml
    Code (CSharp):
    1. <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
     
  8. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    @paganic 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.
     
  9. anirudha_vfx

    anirudha_vfx

    Joined:
    Oct 12, 2013
    Posts:
    11
    Hi.
    It seems to be in c#.
    Can I use this in my javascript app?


    Thanks.
     
  10. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    Yes you can, but you need to put the C# scripts in the Plugins folder, so that they get compiled before the javascripts
     
  11. ramyoraby

    ramyoraby

    Joined:
    Apr 7, 2015
    Posts:
    8
    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 !
     
  12. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    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.
     
  13. ramyoraby

    ramyoraby

    Joined:
    Apr 7, 2015
    Posts:
    8
    Thank you, everything is working fine now :)
     
  14. RussellT

    RussellT

    Joined:
    Feb 1, 2015
    Posts:
    18
    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
     
  15. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    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;
     
  16. RussellT

    RussellT

    Joined:
    Feb 1, 2015
    Posts:
    18
    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
     
  17. jpatinop80

    jpatinop80

    Joined:
    Jul 29, 2012
    Posts:
    55
    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!!!
     
  18. maxkofford

    maxkofford

    Joined:
    Sep 29, 2016
    Posts:
    3
    Seems to have a problem somewhere. This xml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <test>
    <testinner>
    <node id="out1" >
    <node id="in1">

    </node>
    </node>
    <node id="out2" >

    </node>
    <node id="out3" >

    </node>

    </testinner>
    </test>


    Doesn't get parsed correctly. The out3 node ends up inside the out1 node.
     
  19. maxkofford

    maxkofford

    Joined:
    Sep 29, 2016
    Posts:
    3
    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."))
     
  20. Stefan-3DBetrieb

    Stefan-3DBetrieb

    Joined:
    Feb 28, 2017
    Posts:
    16
    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
     
  21. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Hi
    I need to dynamically load text data at runtime locally (same location as app) that contains font and style tags (html markup).
    Would either your SimpleXMl or SimpleJSON assets allow this?
    If so is there a demo for either asset that demonstrates this functionality?
    Also are both assets fully compatible with latest Unity?
    Thanks!
     
    Last edited: Sep 18, 2018