Skip to content

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

@MatthewGitter

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.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions