x


Accessing XML through www

Hi!

I'm looking for a quick solution to accessing an XML file through a WWW request.

My WWW request returns the text of an XML, and i'd want to use it without having it distorted(I'll explain distortion below).

    WWW www = new WWW(url);          
    while(!www.isDone)
    {       
       yield return new WaitForSeconds(0.1f);
    }

    string wwwtext = ASCIIEncoding.ASCII.GetString(Encoding.Convert(Encoding.UTF8,Encoding.ASCII,www.bytes));  

    Debug.Log("new xmldoc");     
    XmlDocument result = new XmlDocument();


    Debug.Log("loadxml");   
    try
    {
       result.LoadXml(wwwtext); 
    }
    catch{}       


    Debug.Log(wwwtext);    

    Debug.Log(result.InnerXml.ToString());
    return false;

So here, i get the xml text which can't be parsed to the XmlDocument, giving me an exception. When I print the text i get back from the WWW, it's different from the XML source, for example:

<resp stat="ok" version="2.0">

became

{"resp": {"status": true, "search":

What can I do to get the original xml source?

Thanks for the help!

more ▼

asked Mar 17 '12 at 03:02 AM

rain gravatar image

rain
0 2 2 2

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Um, It doesn't look like the server you are trying to get xml from is giving you xml.

The format that looks like what you have: {"resp": {"status": true, "search": ect is called JSON a lot of people are using it now a days instead of XML since the files tend to be much smaller and faster to send over the internet.

If it is your own server that you are trying to get data from, try and figure out why your framework is giving you JSON and not XML.

If it is not your server or if you can't figure it out, its not a big deal either, JSON tends to be easier to work with the XML anyway you just need to find a JSON library to help you get at the data.

I would recommend litJSON, its a nice simple open source .net JSON library that works very well with unity. Just drop the dll in your project read through its docs a bit and your should be good to go.

I have a lot of experience loading both XML and JSON data into Unity, but I know relatively little of the server side aspects of it (I usually pay someone to setup the web api for me) so I will not be of much help in try to help you configure your server if that is the problem.

more ▼

answered Mar 17 '12 at 04:32 AM

JDPennock gravatar image

JDPennock
171 1

oh just to add a note, litJSON has failed on me a bit when I try to parse really huge files, but for smaller normalish sized stuff it works fine. Just thought I'd let you know.

Mar 17 '12 at 04:35 AM JDPennock
(comments are locked)
10|3000 characters needed characters left

Thank you very much for the quick and detailed answer.

more ▼

answered Mar 17 '12 at 05:40 PM

rain gravatar image

rain
0 2 2 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x527
x256
x10

asked: Mar 17 '12 at 03:02 AM

Seen: 597 times

Last Updated: Mar 17 '12 at 05:40 PM