我需要向我的项目的api发送xml请求。但是出于安全原因,我无法在程序中编写直接xml的代码。因此,我决定使用jsonix将JSON封送至xml,并取消封送请求的响应。但是我有一些问题。
这是我的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>
由此,我使用jsonix-schema-compiler模块生成了映射:
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;
}
}
在我的主要代码中,我将这样整理xml请求:
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);
在console.log()中,而不是向我显示类似的内容,这就是我想要的结果:
<api><authentication><login>mylogin</login><password>mypassword</password></authentication><command><myfunction><name>test</name></myfunction></command></api>
它正在返回这样的内容:
<api><authentication><login>mylogin</login><password>mypassword</password></authentication><command/></api>
tag命令始终为空,我不明白我在做什么错。 谢谢您的帮助