x


Upload & Download XML files

Hi,

I'm trying to create a XMLHelper class for my game where I want to save and load XML's files from my server.

I would like to know how to upload and download this XMLs from there.

Thanks for all!

more ▼

asked Jan 19 '10 at 04:10 PM

SeRoX gravatar image

SeRoX
57 6 6 12

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

2 answers: sort voted first

Grabbing and sending XML data to and from a server can be done with the WWW class to call scripts on your server.

Since XML is just text, you can use the basic WWW functions to call a URL, and read the 'WWW.data' variable to get your XML. EG:

// Get some xml from our server side script
var url = "http://example.com/myScript.php";

function Start () {

    // Start a download of the given URL
    var www : WWW = new WWW (url);

    // Wait for download to complete
    yield www;

    // put xml into a new string varible
    var myXML = www.data;
}

Similarly, you can use the WWWForm class to POST some XML data as a string to a script on your server. Assuming "myXMLString" contains the XML data that you want to post to your server, you could use this:

var url="http://example.com/anotherScript.php";

function Start() {

    // Create a form object for sending high score data to the server
    var form = new WWWForm();

    // The score
    form.AddField( "data", myXMLString );

    // Create a download object
    var download = new WWW( url, form );

    // Wait until the download is done
    yield download;

    if(download.error) {
            print("Error submitting XML: " + download.error );
    }  else {
            print("data was successfully posted!");
    }
}

As for actually parsing and manipulating the XML data once you have it in Unity, you'll need to use the .net System.Xml classes and functions. Because this is part of .net, and not specific to Unity, you can find out more by doing general searches for how to use XML in .net. The examples will be c#, but the API is exactly the same if you're using Javascript in Unity.

more ▼

answered Jan 19 '10 at 05:25 PM

duck gravatar image

duck ♦♦
41k 92 148 415

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

To download your XML files, just use

var www : WWW = new WWW (url);

// Wait for download to complete
yield www;

and then use www.data or www.bytes, to access the data, which you can then process as XML (for example using the .NET Xml classes).

Uploading data (if you want to use http) works the same way, there's a version of the WWW constructor which takes data to upload as a second parameter:

WWW (url : string, postData : byte[]) : WWW

more ▼

answered Jan 19 '10 at 05:24 PM

jonas echterhoff gravatar image

jonas echterhoff ♦♦
9.8k 7 23 104

(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:

x5077
x413
x178

asked: Jan 19 '10 at 04:10 PM

Seen: 5472 times

Last Updated: Jan 19 '10 at 04:10 PM