load and parse xml from webserver for webgl

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Xml;


public class createPalette : MonoBehaviour {

    public GameObject myPalettePrefab;
    GameObject myPalettePrefabClone;
    public float paletteposx;
    public float paletteposy;
    public float paletteposz;
    public string posxText;
    public string posyText;
    public string poszText;
    public string data;
    public string url = "http://beispiel.de/beispiel.xml";


    public void OnCreatePalette()
    {
        //string data = "beispiel.xml";   //works with xml in Assets
        //string data = "http://beispiel.de/beispiel.xml";  //works with xml in Win-exe but not with webgl
        //string data = System.IO.Path.Combine(Application.streamingAssetsPath, "beispiel.xml"); //works with xml in Win-exe but not with webgl
       
        GetFile();
       parseXmlFile(data);

    }

    IEnumerator GetFile()
    {
        WWW data = new WWW(url);
        yield return data;       
    }



    public void parseXmlFile(string xmlData)
    {
        if (posxText == "")
            posxText = "0";
        if (posyText == "")
            posyText = "0";
        if (poszText == "")
            poszText = "0";


        XmlDocument xmlDoc = new XmlDocument();
        //xmlDoc.Load(new StringReader(xmlData));
        xmlDoc.Load(xmlData);

        string xmlPathPattern = "//lager/palette";
        XmlNodeList myNodeList = xmlDoc.SelectNodes(xmlPathPattern);
        foreach (XmlNode node in myNodeList)
        {
            XmlNode posx = node.FirstChild;
            XmlNode posy = posx.NextSibling;
            XmlNode posz = posy.NextSibling;

            paletteposx = float.Parse(posx.InnerXml);
            paletteposy = float.Parse(posy.InnerXml);
            paletteposz = float.Parse(posz.InnerXml);

            //Debug.Log(paletteposx);

          myPalettePrefabClone = Instantiate(myPalettePrefab, transform.position = new Vector3(paletteposx, paletteposy, paletteposz), Quaternion.Euler(0, 180, 0)) as GameObject;

        }
    }
}

What am I doing wrong?
Thanks a lot,
stefan

Using xml. :stuck_out_tongue:

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.

public string xmlData;

IEnumerator GetFile()
    {
        WWW data = new WWW(url);
        yield return data;

        xmlData = data.text;
    }

basically something along those lines. And of course, run the xmlData through your xmlDoc, etc.

Hi Brathnann,

thanks a lot for helping out of mess :slight_smile:
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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Xml;

public class createPalette : MonoBehaviour {

    public GameObject myPalettePrefab;
    GameObject myPalettePrefabClone;
    public float paletteposx;
    public float paletteposy;
    public float paletteposz;
    public string posxText;
    public string posyText;
    public string poszText;
    public string data;
    public string xmlData;
    public string url = "http://beispiel.de//beispiel.xml";


    public void OnCreatePalette()
    {
        GetFile();
        parseXmlFile(data);
    }

    IEnumerator GetFile()
    {
          WWW data = new WWW(url);
        yield return data;
        xmlData = data.text;
    }

    public void parseXmlFile(string xmlData)
    {
        if (posxText == "")
            posxText = "0";
        if (posyText == "")
            posyText = "0";
        if (poszText == "")
            poszText = "0";


        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlData);

        string xmlPathPattern = "//lager/palette";
        XmlNodeList myNodeList = xmlDoc.SelectNodes(xmlPathPattern);
        foreach (XmlNode node in myNodeList)
        {
            XmlNode posx = node.FirstChild;
            XmlNode posy = posx.NextSibling;
            XmlNode posz = posy.NextSibling;

            paletteposx = float.Parse(posx.InnerXml);
            paletteposy = float.Parse(posy.InnerXml);
            paletteposz = float.Parse(posz.InnerXml);

            //Debug.Log(paletteposx);

          myPalettePrefabClone = Instantiate(myPalettePrefab, transform.position = new Vector3(paletteposx, paletteposy, paletteposz), Quaternion.Euler(0, 180, 0)) as GameObject;

        }
    }
}

Thanks a lot,
Stefan

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.

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

xmlDoc.LoadXml(xmlData);