Skip to content

How to create, populate and marshal Javascript object from XSD? #199

Closed

Description

@MatthewGitter

Hello. I have a collection of XSD schema files from a REST API. I need to create in my Javascript a new object according to its type's XSD, then serialize it to XML and submit it by HTTP to the remote REST API.

How do I create the new object with undefined values in its fields, starting with the XSD files? I could generate Javascript from the XSD in my development environment; I don't need to convert the XSD to JS at runtime. Then in my script I can populate the object's field with the correct values and send it to the script's REST client code.

I used jsonix-schema-compiler-full.jar pointed at the API's main.xsd file to generate an API/API.js file:
var API_Module_Factory = function () { var API = { name: 'API', typeInfos: [{ [...]

but I don't know how to use that file in my script. I want to do something like:

// Mappings generated by:
// java -jar jsonix-full.jar -d mappings -p API main.xsd
var Jsonix = require ('./Jsonix');
var API = require ('./mappings/API').API;


// Create Jsonix marshaller.
var context = new Jsonix.Context([API]);
var marshaller = context.createMarshaller();


// Populate request object fields.
var request = new API.request();
var request.type = 'register';
var request.username = 'wizard';
var request.password = 'hardtoguess';


// Marshal request to XML.
var requestXML = marshaller.marshalDocument(request);


// Submit request for response and log it.
var response = sendAPIRequestXML(requestXML);
console.log(response.message);

Or some other coding style to populate the fields as per the API's XML schema in the XSD files before marshaling them to XML and then using my function to submit the XML to the API.

Activity

  1. MatthewGitter commented on Aug 11, 2018

    @MatthewGitter
    Author

    Haha, this example is actually the way to do it, as I suddenly realized once "the tao of Jsonix" snapped into my head.

    var request = new API.request(); is not necessary, just var request = {}; or initialize the request object and its properties in a single Javascript object literal JSON. Or combine the techniques as in any Javascript. Or get a sample marshalled object in XML and use Jsonix to unmarshal it to a template Javascript object, and perhaps reset some of its properties if necessary.Then the context and its marshaller will generate the XML according to the API XSD.

    Since Javascript is a weakly typed language, it's a good idea to validate the request JSON against a JSON schema generated with Jsonix. Or validate the XML the marshaller generates against an XML schema generated with Jsonix.

    BTW Jsonix is good for unmarshalling into a Javascript object the response XML that's received as a result of sending the request XML to the API.

  2. self-assigned this
    on Mar 18, 2019
  3. added this to the 2.4.2 milestone on Mar 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions