Jstoxml transform module in node does not parse data in correct structure
I am trying to convert a JSON object to XML in my Node js service with jstoxml module. My input structure:
{
"user": "505723c5750c1fa2177682ed",
"uri": "http://localhost:3000/users/505723c5750c1fa2177682ed/items",
"items": [
{
"uri": "http://localhost:3000/items/1"
},
{
"uri": "http://localhost:3000/items/2"
}
],
"info": "blah."
}
Expected Result:
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<items>
<uri>http://localhost:3000/items/2</uri>
</items>
<info>blah.</info>
The result I get is:
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
<uri>http://localhost:3000/items/2</uri>
</items>
<info>blah.</info>
If anyone has faced similar problem please help me. Is there any other npm Node package to get this in the expected structure?
thank
I got a solution for my question. Here you can get it. this is an online demo. By exporting the code they provided, we can achieve it,
Disclaimer: I am the author of Jsonix , an open source JavaScript library for XML β JS transform. Jsonix is ββavailable for Node.js (see here ).
Jsonix can convert between XML and JSON (in both directions) based on the mapping. Mapping gives you the flexibility to convert between JSON and XML. This might be what you need here.
I'll let the code speak. Here's a demo for your JSON-> XML transformation:
- https://github.com/highsource/jsonix/tree/master/nodejs/demos/user
Mapping :
var Mapping = {
name : 'Mapping',
typeInfos : [ {
localName : 'Data',
propertyInfos : [ {
name : 'user'
}, {
name : 'uri'
}, {
name : 'items',
collection : true,
typeInfo : '.Item'
}, {
name : 'info'
} ]
}, {
localName : 'Item',
propertyInfos : [ {
name : 'uri'
} ]
} ],
elementInfos : [ {
elementName : {
localPart : 'data'
},
typeInfo : '.Data'
} ]
};
module.exports.Mapping = Mapping;
Here we have two types: a root type (I called it Data
) and another type Item
. The type is Data
used in the root XML element Data
.
Ok, now the sorting code :
// Create Jsonix context
var context = new Jsonix.Context([ Mapping ]);
var data = {
name : new Jsonix.XML.QName('data'),
value : {
"user" : "505723c5750c1fa2177682ed",
"uri" : "http://localhost:3000/users/505723c5750c1fa2177682ed/items",
"items" : [ {
"uri" : "http://localhost:3000/items/1"
}, {
"uri" : "http://localhost:3000/items/2"
} ],
"info" : "blah."
}
};
var marshaller = context.createMarshaller();
console.log(marshaller.marshalString(data));
And here is the XML you get:
<data>
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<items>
<uri>http://localhost:3000/items/2</uri>
</items>
<info>blah.</info>
</data>
Links:
- Jsonix project on GitHub
- Jsonix Documentation
If you can map your XML to your JSON then check other libraries like xml2js .
Thanks for using my library! I apologize for the delay. You can achieve this output with this structure:
[
{"user": "505723c5750c1fa2177682ed"},
{"uri": "http://localhost:3000/users/505723c5750c1fa2177682ed/items"},
[
{"items": {"uri": "http://localhost:3000/items/1"}},
{"items": {"uri": "http://localhost:3000/items/1"}}
],
{"info": "blah."}
]
Output:
<user>505723c5750c1fa2177682ed</user>
<uri>http://localhost:3000/users/505723c5750c1fa2177682ed/items</uri>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<items>
<uri>http://localhost:3000/items/1</uri>
</items>
<info>blah.</info>