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.

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:

    var uploadedXml = Spark.uploadedXml( Spark.data.uploadId );

When you have obtained this object, you're ready to start processing.

There are 2 models for XML files processing:

Full document processing

When you have obtained the uploadedXml object, you can get the contents of the XML for further processing using the following function:

    var rootElement = uploadedXml.getXml();


Important! The rootElement object is an ECMAScript for XML (E4X) object that can be processed using JavaScript.

Stream Processing

For larger documents, you can let the GameSparks platform read the document, and the platform can callback into a custom function when a particular element path is found. In your callback function you will then call uploadedXml.getElement(); which will give you access to the element that has been located.

E4X Reference

This section shows you how to process the following example XML file:

    <breakfast_menu>
            <starts>06:00</starts>
            <ends>11:00</ends>
        <food id="1" price="5.95">
            <name>Belgian Waffles</name>
            <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
            <calories>650</calories>
        </food>
        <food id="2" price="7.95">
            <name>Strawberry Belgian Waffles</name>
            <description>Light Belgian waffles covered with strawberries and whipped cream</description>
            <calories>900</calories>
        </food>
    </breakfast_menu>

Note: Remember that for this example the rootElement object is an E4X object!

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

    var childElement = rootElement.starts;

To access the text of an element, you can use the text() method:

    var startTime = rootElement.starts.text();

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

    var secondFood = uploadedXml.food[1]

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

    var secondPrice = uploadedXml.food[1].@price

To iterate over attributes, you can use a standard for construct:

    for( var n = 0 ; n < rootElement.food.length() ; n++ )
    {
         var foodElement = rootElement.food[ n ];
    }

Processing JSON Files

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

    var uploadedJson = Spark.uploadedJson( Spark.data.uploadId );

Did this page help you? Please enter your feedback below. For questions about using this part of the platform, please contact support here