XML response serialization for RESTful web service

Hi

RESTful web service response return value is a JavaScript object. This JavaScript object is serialized and placed in the body of the HTTP Response.
Is there way somehow to control this serialization process?
I want the attributes of this JavaScript object to be serialized to XML attributes (Not XML Elements)

Example:

JSON response:

{
    "JMF": {
        "Response": {
            "Type": "SubmissionMethods",
            "ReturnCode": "0",
            "ID": "SubmissionMethodsResponse_Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2",
            "refID": "Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2",
            "SubmissionMethods": {
                "Packaging": "MIME",
                "URLSchemes": "file http"
            }
        }
    }
}

XML Response:

<?xml version="1.0" encoding="UTF-8"?>
<JMF>
    <Response>
        <Type>SubmissionMethods</Type>
        <ReturnCode>0</ReturnCode>
        <ID>SubmissionMethodsResponse_Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2</ID>
        <refID>Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2</refID>
        <SubmissionMethods>
            <Packaging>MIME</Packaging>
            <URLSchemes>file http</URLSchemes>
        </SubmissionMethods>
    </Response>
</JMF>

I want “MIME” to be an attribute of “SubmissionMethods” XML Element etc.

Thanks, Anton

Hi,

In stead of a json object you can also return an xml object where you have full control of which data is serialized as element or as attribute:

return new XML('<JMF>\
    <Response>\
        <Type>SubmissionMethods</Type>\
        <ReturnCode>0</ReturnCode>\
        <ID>SubmissionMethodsResponse_Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2</ID>\
        <refID>Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2</refID>\
        <SubmissionMethods Packaging="MIME">\
            <URLSchemes>file http</URLSchemes>\
        </SubmissionMethods>\
    </Response>\
</JMF>')

Alternatively, you can create the xml programatically:

var xml = new XML('<JMF></JMF>')
xml.Response.Type = 'SubmissionMethods'
xml.Response.ReturnCode = 0
xml.Response.ID = 'SubmissionMethodsResponse_Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2'
xml.Response.refID = 'Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2'
xml.Response.SubmissionMethods.@Packaging = 'MIME'
xml.Response.SubmissionMethods.URLSchemes = 'file http'
return xml

In both cases, this is what is returned to the client:

<?xml version="1.0" encoding="UTF-8"?>
<JMF>
  <Response>
    <Type>SubmissionMethods</Type>
    <ReturnCode>0</ReturnCode>
    <ID>SubmissionMethodsResponse_Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2</ID>
    <refID>Q38BE5CF9-F912-4E18-8F76-C3DB8B9CEEE2</refID>
    <SubmissionMethods Packaging="MIME">
      <URLSchemes>file http</URLSchemes>
    </SubmissionMethods>
  </Response>
</JMF>