Piko
Piko

Reputation: 11

Marshalling JSON to XML with jsonix gives invalide XML

I need to send a xml request to an api for my project. But for security reasons, I cannon't code direct xml in my program. So I decided to use jsonix to marshal JSON to xml, and unmarshal the respond of the request. But I have some problemes.

Here is my xml:

<?xml version = '1.0' encoding='UTF-8'?>
<api>
    <authentication>
        <login>MyLogin</login>
        <password>MyPassword</password>
    </authentication>
    <command>
        <myfunction>
            <name>test</name>
        </myfunction>
     </command>
</api>

With this, I have generated the Mappings with the jsonix-schema-compiler module:

var Search_Module_Factory = function () {
	var Search = {
		name: "Search",
		typeInfos: [{
			localName: "Api.Command",
			typeName: null,
			propertyInfos: [{
				name: "myFunction",
				required: true,
				elementName: {
					localPart: "myfunction"
				},
				typeInfo: ".Api.Command.MyFunction"
			}]
		}, {
			localName: "Api.Authentication",
			typeName: null,
			propertyInfos: [{
				name: "login",
				required: true,
				elementName: {
					localPart: "login"
				}
			}, {
				name: "password",
				required: true,
				elementName: {
					localPart: "password"
				}
			}]
		}, {
			localName: "Api.Command.MyFunction",
			typeName: null,
			propertyInfos: [{
				name: "name",
				required: true,
				elementName: {
					localPart: "name"
				}
			}]
		}, {
			localName: "Api",
			typeName: null,
			propertyInfos: [{
				name: "authentication",
				required: true,
				elementName: {
					localPart: "authentication"
				},
				typeInfo: ".Api.Authentication"
			}, {
				name: "command",
				required: true,
				elementName: {
					localPart: "command"
				},
				typeInfo: ".Api.Command"
			}]
		}],
		elementInfos: [{
			typeInfo: ".Api",
			elementName: {
				localPart: "api"
			}
		}]
	};
	return {
		Search: Search
	};
};
if (typeof define === "function" && define.amd) {
	define([], Search_Module_Factory);
}
else {
	var Search_Module = Search_Module_Factory();
	if (typeof module !== "undefined" && module.exports) {
		module.exports.Search = Search_Module.Search;
	}
	else {
		var Search = Search_Module.Search;
	}
}

In my main code, I am marshalling the xml request like this:

var Search = require("./mappings/Search").Search;
var context = new Jsonix.Context([ Search]);
var marshaller = context.createMarshaller();

var originalJS = {
			"api": {
				"authentication": {
					"login" :"mylogin",
					"password": "mypassword",
				},
				"command":{
					"myfunction":{
						"name": "test"
					}
				},
			}
		};
var marshalledXML = marshaller.marshalString(originalJS);
console.log(marshalledXML);

And in the console.log(), instead of showing me something like this, which is the result I want:

<api><authentication><login>mylogin</login><password>mypassword</password></authentication><command><myfunction><name>test</name></myfunction></command></api>

It's returning something like this:

<api><authentication><login>mylogin</login><password>mypassword</password></authentication><command/></api>

The tag command is always empty, and I don't understand what I'm doing wrong. Thank for your help

Upvotes: 0

Views: 213

Answers (0)

Related Questions