How to process XML and JSON files

You can use Cloud Code to process both XML files and JSON files, these may be delivered by a player using the GetUploadUrlRequest model, or be delivered via SFTP to the server from a third party.

Processing XML Files

Both UploadCompleteMessage, and the “File Delivered” Cloud Code scripts have access to an attribute that allows you to get the uploaded file so you can process it:

Once you have this object, you are ready to start processing. There are 2 models for xml files processing, full document and streaming. Use streaming when your document might be large, as loading the whole document into memory can take some time.

Full document processing

Once you have the uploadedXml object you can get the contents of the XML for further processing using the following function;

The rootElement object is an E4X object that can be processed using javascript;

Stream Processing

For larger documents, you can let the GameSparks platform read the document, and it can callback into a custom function when a particular element path is found.

If you callback function you will then call  uploadedXml.getElement(); which will give you access to the element that has been located. It should be noted that this method will

Processing JSON Files

JSON processing is always in full document mode, once you have the handle on the JSON document you can use it as you would any JSON object.

E4X Reference

This section will show how to process an example XML file, the file used in the example is as follows:

To access an element you can use dot notation to navigate through the document.

To access the text of an element use the text() method

For repeating items, you can use array notation (0 based) to access an element by index.

To access an attribute, you can use the @symbol as the final part of your dot notation



Who uses GameSparks?