Read XML without document element

Discussion in 'Scripting' started by giannis, Apr 13, 2012.

  1. giannis

    giannis

    New Member

    Joined:
    Mar 6, 2012
    Messages:
    32
    Hi all

    I am trying to load XML in unity without a document element (I get it from a web service). The XML has the following form:

    Code (csharp):
    1.  
    2. <virtualpatientscript>
    3.   <act id="Introduction">
    4.     <triggermap>
    5.       <trigger_map_id>12886</trigger_map_id>
    6.       <responses>
    7.         <speech>Social_3.hap</speech>
    8.       </responses>
    9.       <triggers>
    10.       </triggers>
    11.       <topic>Social</topic>
    12.       <sentences>
    13. </virtualpatientscript>
    14.  
    15.  
    16. Unity returns the following error which i think is related to the way the document is constructed.  Is there any way to bypass that?
    17.  
    18. XmlException: Document element did not appear.  Line 1, position 1.
    19. Mono.Xml2.XmlTextReader.Read ()
     
  2. diablo

    diablo

    New Member

    Joined:
    Jan 3, 2011
    Messages:
    736
    Dude, stop posting this over and over. Once is enough!
     
  3. goat

    goat

    Member

    Joined:
    Aug 24, 2009
    Messages:
    2,985
    Well what web service returned that to you?

    <triggermap> is not closed. <sentences> is not closed.

    So you want to tell Unity to ignore a broken document?
     
  4. giannis

    giannis

    New Member

    Joined:
    Mar 6, 2012
    Messages:
    32
    This is the complete XML file. You can call it broken I guess.. Visual studio can read it just fine though. Is there anything I can do?

    <virtualpatientscript>
    <act id="Introduction">
    <triggermap>
    <trigger_map_id>12886</trigger_map_id>
    <responses>
    <speech>Social_3.hap</speech>
    </responses>
    <triggers>
    </triggers>
    <topic>Social</topic>
    <sentences>
    <sentence>Hi Simone it is nice to meet you too</sentence>
    <sentence>
    </sentence>
    <sentence>Hi Simone it is a pleasure to meet you</sentence>
    <sentence>Hello there pleasure to meet you</sentence>
    <sentence>Hi Simone pleasure to meet you</sentence>
    </sentences>
    </triggermap>
    <triggermap>
    <trigger_map_id>12889</trigger_map_id>
    <responses>
    <speech>Use_3.hap</speech>
    </responses>
    <triggers>
    </triggers>
    <topic>Use_of_system</topic>
    <sentences>
    <sentence>Which attractions of the castle are we going to visit</sentence>
    <sentence>I want more information for our current route</sentence>
    </sentences>
    </triggermap>
    <triggermap>
    <trigger_map_id>12890</trigger_map_id>
    <responses>
    <speech>Use_5.hap</speech>
    </responses>
    <triggers>
    </triggers>
    <topic>Use_of_system</topic>
    <sentences>
    <sentence>if I get lost is there anyway to help me</sentence>
    <sentence>if I get lost what happens then</sentence>
    <sentence>if I get lost will you be able to help me</sentence>
    <sentence>What happens if I get lost on a route</sentence>
    </sentences>
    </triggermap>
    <triggermap>
    <trigger_map_id>12926</trigger_map_id>
    <responses>
    <speech>Social_1.hap</speech>
    <br />
    <b>Warning</b>: mysql_fetch_array() expects parameter 1 to be resource, boolean given in <b>C:\xampp\htdocs\VirtualPeopleFactory\xml_script_exporter.php</b> on line <b>179</b><br /><morph name="" weight="0.30" delay="0" rampup="500" stable="1000" rampdown="500" /></responses>
    <triggers>
    </triggers>
    <topic>Social</topic>
    <sentences>
    <sentence>Hi Simone it is a beautiful day for a walk in the castle do not you think</sentence>
    <sentence>What a great day for a walk in the castle</sentence>
    </sentences>
    </triggermap>
    <triggermap>
    <trigger_map_id>12927</trigger_map_id>
    <responses>
    <speech>Use_4.hap</speech>
    </responses>
    <triggers>
    </triggers>
    <topic>Use_of_system</topic>
    <sentences>
    <sentence>Why can not I create my own routes for the castle</sentence>
    <sentence>Why can not I choose the locations to visit myself</sentence>
    <sentence>Why can not I simply choose what to see</sentence>
    </sentences>
    </triggermap>
    <triggermap>
    <trigger_map_id>12928</trigger_map_id>
    <responses>
    <speech>Use_6.hap</speech>
    </responses>
    <triggers>
    </triggers>
    <topic>Use_of_system</topic>
    <sentences>
    <sentence>Can you repeat your explanation on how to use the system</sentence>
    </sentences>
    </triggermap>
    <triggermap>
    <trigger_map_id>12929</trigger_map_id>
    <responses>
    <speech>Social_2.hap</speech>
    </responses>
    <triggers>
    </triggers>
    <topic>Social</topic>
    <sentences>
    <sentence>Hi Simone</sentence>
    <sentence>Hi there</sentence>
    <sentence>Hello Simone</sentence>
    <sentence>Hey there</sentence>
    </sentences>
    </triggermap>
    <triggermap>
    <trigger_map_id>12932</trigger_map_id>
    <responses>
    <speech>Use_4_3.hap</speech>
    </responses>
    <triggers>
    </triggers>
    <topic>Use_of_system</topic>
    <sentences>
    <sentence>No problem you seem quite capable I am sure I will enjoy the tour</sentence>
    <sentence>No problem you seem to know what you are doing and I am sure I will enjoy the tour</sentence>
    </sentences>
    </triggermap>
    <triggermap>
    <trigger_map_id>12933</trigger_map_id>
    <responses>
    <speech>Use_4_1.hap</speech>
    </responses>
    <triggers>
    </triggers>
    <topic>Use_of_system</topic>
    <sentences>
    <sentence>Hm can I trust my entertainment to your hands</sentence>
    <sentence>Can I trust your judgement </sentence>
    </sentences>
    </triggermap>
    <triggermap>
    <trigger_map_id>12934</trigger_map_id>
    <responses>
    <speech>Use_5_1.hap</speech>
    </responses>
    <triggers>
    </triggers>
    <topic>Use_of_system</topic>
    <sentences>
    <sentence>why can not you help me if I get lost</sentence>
    </sentences>
    </triggermap>
    <triggermap>
    <trigger_map_id>12937</trigger_map_id>
    <responses>
    <speech>Use_7.hap</speech>
    </responses>
    <triggers>
    </triggers>
    <topic>Use_of_system</topic>
    <sentences>
    <sentence>I would like to start the tour please</sentence>
    <sentence>
    </sentence>
    <sentence>Great let us begin then</sentence>
    </sentences>
    </triggermap>
    <triggermap>
    <trigger_map_id>12939</trigger_map_id>
    <responses>
    <speech>Use_4_2.hap</speech>
    </responses>
    <triggers>
    </triggers>
    <topic>Use_of_system</topic>
    <sentences>
    <sentence>
    </sentence>
    <sentence>I would prefer to choose the locations to visit myself</sentence>
    <sentence>OK but still I would like to choose what to see myself</sentence>
    </sentences>
    </triggermap>
    <triggermap>
    <trigger_map_id>12940</trigger_map_id>
    <responses>
    <speech>Use_1.hap</speech>
    </responses>
    <triggers>
    </triggers>
    <topic>Use_of_system</topic>
    <sentences>
    <sentence>Yes please go on</sentence>
    <sentence>
    </sentence>
    <sentence>Yes please I have no idea how the system works</sentence>
    </sentences>
    </triggermap>
    <triggermap>
    <trigger_map_id>12941</trigger_map_id>
    <responses>
    <speech>Use_2.hap</speech>
    </responses>
    <triggers>
    </triggers>
    <topic>Use_of_system</topic>
    <sentences>
    <sentence>I feel pretty comfortable with the use of the system</sentence>
    <sentence>
    </sentence>
    <sentence>No I know what I have to do </sentence>
    </sentences>
    </triggermap>
    <triggermap>
    <trigger_map_id>17752</trigger_map_id>
    <responses>
    <speech>Social_4.hap</speech>
    </responses>
    <triggers>
    </triggers>
    <topic>Social</topic>
    <sentences>
    <sentence>Hello beautiful how are you today</sentence>
    <sentence>Hello sexy how you doing</sentence>
    </sentences>
    </triggermap>
    </act>
    <act id="No Act Set">
    </act>
    <replacementlist>
    <map>
    <phrase>show</phrase>
    <replacewith>attraction</replacewith>
    </map>
    <map>
    <phrase>knowledge</phrase>
    <replacewith>information</replacewith>
    </map>
    <map>
    <phrase>info</phrase>
    <replacewith>information</replacewith>
    </map>
    <map>
    <phrase>knowing</phrase>
    <replacewith>know</replacewith>
    </map>
    <map>
    <phrase>how-do-you-do</phrase>
    <replacewith>hi</replacewith>
    </map>
    <map>
    <phrase>howdy</phrase>
    <replacewith>hi</replacewith>
    </map>
    <map>
    <phrase>Hawaii</phrase>
    <replacewith>hi</replacewith>
    </map>
    <map>
    <phrase>hullo</phrase>
    <replacewith>hi</replacewith>
    </map>
    <map>
    <phrase>Hawai'i</phrase>
    <replacewith>hi</replacewith>
    </map>
    <map>
    <phrase>Aloha State</phrase>
    <replacewith>hi</replacewith>
    </map>
    <map>
    <phrase>HI</phrase>
    <replacewith>hi</replacewith>
    </map>
    <map>
    <phrase>greeting</phrase>
    <replacewith>hi</replacewith>
    </map>
    <map>
    <phrase>salutation</phrase>
    <replacewith>hi</replacewith>
    </map>
    <map>
    <phrase>Begin</phrase>
    <replacewith>begin</replacewith>
    </map>
    <map>
    <phrase>glad</phrase>
    <replacewith>nice</replacewith>
    </map>
    <map>
    <phrase>to meet</phrase>
    <replacewith>to see</replacewith>
    </map>
    </replacementlist>
    </virtualpatientscript>
     
    Last edited: Apr 13, 2012
  5. goat

    goat

    Member

    Joined:
    Aug 24, 2009
    Messages:
    2,985
    Add this (if you didn't leave it off because you felt it was obvious):

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

    So I would start with that.

    If that didn't fix it take a well known public XML schema and a short XML file generated from that XML schema and see if Unity handles it.

    As I really don't know your approach here is a couple of threads to help you out:

    (example simple test XML)
    http://files.unity3d.com/tom/orbitsim/XML/SolarSystem.xml
    the thread it's from:
    http://forum.unity3d.com/threads/25352-Loading-an-XML-file

    and then a more recent thread giving another approach:

    http://forum.unity3d.com/threads/131564-XmlReader-for-WebPlayer-MethodAccessException

    I had fits building a WSDL and generating working XML from the Amazon Seller Central XML schemas in Eclipse because of all the nested complex types in those schemas.

    Then supposing that is fine then give Unity your XML schema and a shorter simplified XML output file and see if Unity can handle that.
     
  6. goat

    goat

    Member

    Joined:
    Aug 24, 2009
    Messages:
    2,985
    I just noticed: It looks like a mismatch between the db field datatype and the XML data type.

    <br />
    <b>Warning</b>: mysql_fetch_array() expects parameter 1 to be resource, boolean given in <b>C:\xampp\htdocs\VirtualPeopleFactory\xml_script _exporter.php</b> on line <b>179</b><br /><morph name="" weight="0.30" delay="0" rampup="500" stable="1000" rampdown="500" />
     
  7. giannis

    giannis

    New Member

    Joined:
    Mar 6, 2012
    Messages:
    32
    OK I tried a different script with no problems this time. Unity returns the same error. My XML parsing is been handled by a dll API provided by the service provider. The idea is to add natural language capabilities to NPCs

    <virtualpatientscript>
    <act id="No Act Set">
    <triggermap>
    <trigger_map_id>63236</trigger_map_id>
    <responses>
    <speech>Hi am talking</speech>
    </responses>
    <triggers>
    </triggers>
    <topic>unassigned</topic>
    <sentences>
    <sentence>Hello there</sentence>
    </sentences>
    </triggermap>
    <triggermap>
    <trigger_map_id>63237</trigger_map_id>
    <responses>
    </responses>
    <triggers>
    </triggers>
    <topic>unassigned</topic>
    <sentences>
    </sentences>
    </triggermap>
    </act>
    <replacementlist>
    </replacementlist>
    </virtualpatientscript>
     
  8. giannis

    giannis

    New Member

    Joined:
    Mar 6, 2012
    Messages:
    32
    OK apparently unity has no native XML support (shame really). This means that I need to parse the xml data through some external solution. Not good for unity I think as XML is been used everywhere
     
  9. KelsoMRK

    KelsoMRK

    Member

    Joined:
    Jul 18, 2010
    Messages:
    3,021
    You have access to System.Xml - not sure what you need beyond that.
     
  10. diablo

    diablo

    New Member

    Joined:
    Jan 3, 2011
    Messages:
    736
    Unity doesn't need native support for XML; the .NET framework already provides it. There are numerous examples online of how to read full and partial xml in c#, so I don't understand what the issue is. Why don't you post the code that's giving you an error?
     
  11. giannis

    giannis

    New Member

    Joined:
    Mar 6, 2012
    Messages:
    32
    The XML is been parsed through a dll provided by the web service. hr.getScript(currentscript); gets the script (you can see an example here http://vpf.cise.ufl.edu/VirtualPeopleFactory/xml_script_exporter.php?script_id=710). Visual studio works fine but unity doesnt like it. its shame because I would like to add NLP to NPCs. It would be fun to talk to NPCs usign text instead of multiple choice menus.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using System.Runtime.InteropServices;
    7. using VPFCommunicationsLibrary;
    8. using System.Text;
    9. using System.Collections.Specialized;
    10.  
    11. public class NLP : MonoBehaviour
    12. {
    13.     private VPFNaturalLanguageCommunicator hr = new VPFNaturalLanguageCommunicator();
    14.  
    15.  
    16.     void Start ()
    17.     {
    18.         hr.setAct("");
    19.         hr.setPreviousTopic("Say Hello");
    20.         hr.setHost("http://verg.cise.ufl.edu/VirtualPeopleFactory");
    21.         hr.setNumResponsesPerInput(10);
    22.  
    23.     }
    24.    
    25.     void Update () {
    26.     if (Input.GetKey(KeyCode.S)) {
    27.    windowopen = true;
    28.  
    29.                     }      
    30. }
    31. string response;
    32. bool windowopen = false;
    33. string currentscript = "710";
    34.    
    35. void OnGUI() {
    36.         if (windowopen){   
    37.         if (GUI.Button(new Rect(10, 10, 50, 50), "test"))
    38.         response = hr.getScript(currentscript);
    39.     print (response);}
    40.     }
    41. }
    42.  
     
  12. giannis

    giannis

    New Member

    Joined:
    Mar 6, 2012
    Messages:
    32
    Anybody out there? Any ideas?
     
  13. diablo

    diablo

    New Member

    Joined:
    Jan 3, 2011
    Messages:
    736
    I believe you may be encountering an issue where the dll is doing something underneath the scenes that isn't compatible in mono. Try catching the exception and checking all the inner exceptions to see if there are other errors lurking underneath
     
  14. giannis

    giannis

    New Member

    Joined:
    Mar 6, 2012
    Messages:
    32
    The dll works fine.. I've used it several times in Visual studio. The problem is with Unity
     
  15. KelsoMRK

    KelsoMRK

    Member

    Joined:
    Jul 18, 2010
    Messages:
    3,021
    Just because they work in Visual Studio doesn't mean they work everywhere. My guess is you're compiling against .NET in Visual Studio whereas Unity is compiling against Mono.

    I would again ask - why the plugin? You have System.Xml at your disposal so parse the data with that. Or better yet - try to parse it with your plugin, catch the exception (if any), and then parse it with System.Xml instead.
     
  16. giannis

    giannis

    New Member

    Joined:
    Mar 6, 2012
    Messages:
    32
    Hi

    thanks for your input...

    <<try to parse it with your plugin, catch the exception (if any), and then parse it with System.Xml instead.>>

    How do it do that? Can you give me a specific example based on my code?
     
  17. goat

    goat

    Member

    Joined:
    Aug 24, 2009
    Messages:
    2,985
    Have you tried what's in this thread?

    http://forum.unity3d.com/threads/131...ccessException

    and for appearance's sake you really should identify the file as XML with this line:

    <?xml version="1.0" encoding="utf-8"?>
     
  18. diablo

    diablo

    New Member

    Joined:
    Jan 3, 2011
    Messages:
    736
    The dll works fine when just in time compiled within visual studio, but when jit'd in Unity's mono i'm pretty sure it's failing. Like I said before, check all the inner exceptions.
     
  19. goat

    goat

    Member

    Joined:
    Aug 24, 2009
    Messages:
    2,985
    http://books.xmlschemata.org/relaxng/ch19-77151.html

    The lexical domain (xsd:NCName) of this datatype doesn't allow the definition of numerical identifiers or identifiers containing whitespace.

    By the way, I put your XML in a couple of online XML Schema generators and they didn't catch this.

    I have found to my frustration most of these things aren't caught by a parser til you try to use them at runtime, kind of like shell scripting.

    The safest thing I can recommend is never use white space in any XML name/attribute/type/id/etc... (I didn't go look up all the XML rules to verify this. The whitespace as value data within the tags is OK.)
     
    Last edited: Apr 15, 2012