Search Unity

load and parse xml from webserver for webgl

Discussion in 'Scripting' started by Stefan-3DBetrieb, Jul 26, 2017.

  1. Stefan-3DBetrieb

    Stefan-3DBetrieb

    Joined:
    Feb 28, 2017
    Posts:
    16
    Hi together,
    I am new to scripting at first.
    I try to build a webgl game wich places gameobjects onClick event on certain positions stored in a xml-file.
    The xml file is located in the web so it can get modified.
    When I attatch my xml in the local asset folder, everything works fine.
    Only the load and parse xml from the webserver wont work.
    Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System.IO;
    6. using System.Xml;
    7.  
    8.  
    9. public class createPalette : MonoBehaviour {
    10.  
    11.     public GameObject myPalettePrefab;
    12.     GameObject myPalettePrefabClone;
    13.     public float paletteposx;
    14.     public float paletteposy;
    15.     public float paletteposz;
    16.     public string posxText;
    17.     public string posyText;
    18.     public string poszText;
    19.     public string data;
    20.     public string url = "http://beispiel.de/beispiel.xml";
    21.  
    22.  
    23.     public void OnCreatePalette()
    24.     {
    25.         //string data = "beispiel.xml";   //works with xml in Assets
    26.         //string data = "http://beispiel.de/beispiel.xml";  //works with xml in Win-exe but not with webgl
    27.         //string data = System.IO.Path.Combine(Application.streamingAssetsPath, "beispiel.xml"); //works with xml in Win-exe but not with webgl
    28.        
    29.         GetFile();
    30.        parseXmlFile(data);
    31.  
    32.     }
    33.  
    34.     IEnumerator GetFile()
    35.     {
    36.         WWW data = new WWW(url);
    37.         yield return data;      
    38.     }
    39.  
    40.  
    41.  
    42.     public void parseXmlFile(string xmlData)
    43.     {
    44.         if (posxText == "")
    45.             posxText = "0";
    46.         if (posyText == "")
    47.             posyText = "0";
    48.         if (poszText == "")
    49.             poszText = "0";
    50.  
    51.  
    52.         XmlDocument xmlDoc = new XmlDocument();
    53.         //xmlDoc.Load(new StringReader(xmlData));
    54.         xmlDoc.Load(xmlData);
    55.  
    56.         string xmlPathPattern = "//lager/palette";
    57.         XmlNodeList myNodeList = xmlDoc.SelectNodes(xmlPathPattern);
    58.         foreach (XmlNode node in myNodeList)
    59.         {
    60.             XmlNode posx = node.FirstChild;
    61.             XmlNode posy = posx.NextSibling;
    62.             XmlNode posz = posy.NextSibling;
    63.  
    64.             paletteposx = float.Parse(posx.InnerXml);
    65.             paletteposy = float.Parse(posy.InnerXml);
    66.             paletteposz = float.Parse(posz.InnerXml);
    67.  
    68.             //Debug.Log(paletteposx);
    69.  
    70.           myPalettePrefabClone = Instantiate(myPalettePrefab, transform.position = new Vector3(paletteposx, paletteposy, paletteposz), Quaternion.Euler(0, 180, 0)) as GameObject;
    71.  
    72.         }
    73.     }
    74. }
    75.  
    What am I doing wrong?
    Thanks a lot,
    stefan
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Using xml. :p

    Ok, so in all seriousness. You aren't assigning the data you get back to anything.

    You have a public string called data

    However, you're then creating a local variable in GetFile that is of type WWW

    public string data;
    WWW data;

    These have nothing to do with each other and this does nothing. So, first, rename your string data to something else

    public string xmlData; for example.

    Then, after your yield return data; you need to actually assign the text you get to the variable.

    Code (CSharp):
    1. public string xmlData;
    2.  
    3. IEnumerator GetFile()
    4.     {
    5.         WWW data = new WWW(url);
    6.         yield return data;
    7.  
    8.         xmlData = data.text;
    9.     }
    basically something along those lines. And of course, run the xmlData through your xmlDoc, etc.
     
    Last edited: Jul 27, 2017
  3. Stefan-3DBetrieb

    Stefan-3DBetrieb

    Joined:
    Feb 28, 2017
    Posts:
    16
    Hi Brathnann,

    thanks a lot for helping out of mess :)
    Okay I tryed your solution and have of course new problems.
    At first I created the public string xmlData and your piece of code.
    But it runs to an error with "xmlData = www.text;" "..The name 'www' does not exist in the current context..."

    So I tried to assign the data I got from WWW to the variable xmlData, but it runs to an error too:
    "..The specified path is not of a legal form (empty)..."
    So here is my actual piece of code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System.IO;
    6. using System.Xml;
    7.  
    8. public class createPalette : MonoBehaviour {
    9.  
    10.     public GameObject myPalettePrefab;
    11.     GameObject myPalettePrefabClone;
    12.     public float paletteposx;
    13.     public float paletteposy;
    14.     public float paletteposz;
    15.     public string posxText;
    16.     public string posyText;
    17.     public string poszText;
    18.     public string data;
    19.     public string xmlData;
    20.     public string url = "http://beispiel.de//beispiel.xml";
    21.  
    22.  
    23.     public void OnCreatePalette()
    24.     {
    25.         GetFile();
    26.         parseXmlFile(data);
    27.     }
    28.  
    29.     IEnumerator GetFile()
    30.     {
    31.           WWW data = new WWW(url);
    32.         yield return data;
    33.         xmlData = data.text;
    34.     }
    35.  
    36.     public void parseXmlFile(string xmlData)
    37.     {
    38.         if (posxText == "")
    39.             posxText = "0";
    40.         if (posyText == "")
    41.             posyText = "0";
    42.         if (poszText == "")
    43.             poszText = "0";
    44.  
    45.  
    46.         XmlDocument xmlDoc = new XmlDocument();
    47.         xmlDoc.Load(xmlData);
    48.  
    49.         string xmlPathPattern = "//lager/palette";
    50.         XmlNodeList myNodeList = xmlDoc.SelectNodes(xmlPathPattern);
    51.         foreach (XmlNode node in myNodeList)
    52.         {
    53.             XmlNode posx = node.FirstChild;
    54.             XmlNode posy = posx.NextSibling;
    55.             XmlNode posz = posy.NextSibling;
    56.  
    57.             paletteposx = float.Parse(posx.InnerXml);
    58.             paletteposy = float.Parse(posy.InnerXml);
    59.             paletteposz = float.Parse(posz.InnerXml);
    60.  
    61.             //Debug.Log(paletteposx);
    62.  
    63.           myPalettePrefabClone = Instantiate(myPalettePrefab, transform.position = new Vector3(paletteposx, paletteposy, paletteposz), Quaternion.Euler(0, 180, 0)) as GameObject;
    64.  
    65.         }
    66.     }
    67. }
    68.  
    Thanks a lot,
    Stefan
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Oh, yeah. www.text is a habit.
    I ususally do
    WWW www = new WWW(url); That part is my mistake.

    Get rid of your

    public string data; It's not needed and just creates confusion with your WWW call.

    I don't use xml anymore currently, so I don't recall all the stuff for it. But, it looks like you can't use xmlDoc.Load on a string itself.

    https://msdn.microsoft.com/en-us/library/875kz807(v=vs.110).aspx

    It appears the string version takes a url and not a string of text. I'll be honest, I'm not sure how to load a string of text into the xmlDoc this way.

    try

    Code (CSharp):
    1. xmlDoc.LoadXml(xmlData);