javascriptgoogle-apps-scriptxmldomjsonix

Jsonix in Google Apps Script


How can I use Jsonix in Google Apps Script (server side Google Docs automation)? If I can't, what's an alternate to Jsonix I can use in GAS to generate Javascript mappings from XSD, then serialize a JS object to XML for submission to a REST API, then deserialize the response back into a JS object?

I used Jsonix to compile a REST API from its XSD files to JSON mappings. I tested OK in commandline Node.js a Javascript that creates a JS object from inline JSON, then marshals it under the Jsonix.Context for submisstion to the REST API, then unmarshals the response.

Then I created a Google Apps Script project with my script. I created another GAS project with the Jsonix.js script (as Jsonix.gs), then another GAS project with the mappings script (as API.gs). I configured my main script's project Resources with the Jsonix and API libraries' project keys. I can execute my script (verifying the library resources are used), but it fails when Jsonix.createDocument() is called. I inserted into the beginning of the Jsonix library script's _jsonix_factory = function(_jsonix_xmldom, _jsonix_xmlhttprequest, _jsonix_fs) a Logger.log("_jsonix_xmldom: " + _jsonix_xmldom) line that logs _jsonix_xmldom: undefined , which is why Jsonix.createDocument() throws an error instead of returning a document.

Can I use the Google Apps Script service XmlService, or some other service in the GAS environment, instead of the _jsonix_xmldom that Jsonix expects by default? Or can I include as a library resource some functionally equivalent libraries? Or otherwise supply that functionality to Jsonix in GAS?

I expect that similar errors will result from Jsonix trying to use the _jsonix_xmlhttprequest and _jsonix_fs values that are also undefined. I see in the Jsonix.js (.gs) source code the two lines // REWORK // Node.js in the createDocument() declaration and elsewhere. Perhaps this case I'm reporting is in active development?

Thanks for your insights.


Solution

  • I wound up creating new Google Apps Script projects for Jsonix, for its XMLDOM dependency, and for my API mappings JS (GAS) that I generated with Jsonix from the API's XSD files. I made my GAS Jsonix project depend on my XMLDOM project as a library resource. Then I made a GAS project for my main script, which depends on the Jsonix GAS project and the API GAS project as library resources. It works.

    I used an existing, well tested XMLDOM project with an appropriate FOSS license, and tweaked it to fit the GAS API requirements.

    I tried using the GAS XmlService, wrapping its API to present its relevant members as DOMImplementation, DOMParser and XMLSerializer, but the the XmlService API is deeply inconsistent with the XMLDOM API. The XmlService API's members corresponding to the standard DOM implementation's are structured not at all consistently with the standard API.