Quantcast
Channel: Answers for "Upload & Download XML files"
Viewing all articles
Browse latest Browse all 6

Answer by duck

$
0
0

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.


Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>