A library for fast JSON to XML transformations.
npm install json-xml-parseA library for fast JSON to XML transformations.
* Fast Conversion
* Lightweight
* Dependency-Free
* CLI and Browser Support
| Name | Type | Default
|---|---|---|
| attrKey | str | "@" |
| contentKey | str | "#" |
| beautify | bool | true |
| selfClosing | bool | true |
| typeHandler | func | (v) => [true, v] |
| declaration | obj | { "version": "1.0" } |
| entityMap | obj | { "<": "<", ">": ">", "&": "&", "'": "'", "\"": """} |
``js
const parser = require('json-xml-parse');
const data = {
SolarSystem: {
Galaxy: "\"MilkyWay\"",
Star: () => "Sun",
Planet: [
{
"position": "1",
"name": "
"distance": "58",
},
{
"position": "2",
"name": "Venus",
"distance": "108"
}
],
Planet1: {
"@": {
position: "3",
distance: "149"
},
name: "Earth"
},
Planet2: {
"@": {
position: "4",
distance: 227,
},
"#": {
name: {
"#" : 'mars',
"@": {
"has-water": 'true'
},
},
moon: [
undefined,
'Europa',
'Deimos'
]
}
}
}
}
const xml = parser.jsXml.toXmlString(data);
`
`xml`
`js`
const options = {
typeHandler: (v) => [true, v];
// return [
// runs for every nested property
}
`js
const Options = {
beautify: true,
selfClosing: true,
typeHandler: (v) => {
if([undefined, null, ''].includes(v)) {
return [false, v]
}
return [true, v]
}
}
const data = {
SolarSystem: {
Galaxy: undefined,
Star: () => null,
Planet: 'Earth'
}
}
const xml = parser.jsXml.toXmlString(data, Options);
`
`xml``