1.3.0 • Published 9 months ago
json-xml-parse v1.3.0
Transform JSON to XML
A library for fast JSON to XML transformations.
Main Features
- Fast Conversion
- Lightweight
- Dependency-Free
- CLI and Browser Support
Options
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 | { "<": "<", ">": ">", "&": "&", "'": "'", "\"": """} |
Usage
const parser = require('json-xml-parse');
const data = {
SolarSystem: {
Galaxy: "\"MilkyWay\"",
Star: () => "Sun",
Planet: [
{
"position": "1",
"name": "<Mercury>",
"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 version="1.0"?>
<SolarSystem>
<Galaxy>"MilkyWay"</Galaxy>
<Star>Sun</Star>
<Planet>
<position>1</position>
<name><Mercury></name>
<distance>58</distance>
</Planet>
<Planet>
<position>2</position>
<name>Venus</name>
<distance>108</distance>
</Planet>
<Planet1 position="3" distance="149">
<name>Earth</name>
</Planet1>
<Planet2 position="4" distance="227">
<name has-water="true">mars</name>
<moon/>
<moon>Europa</moon>
<moon>Deimos</moon>
</Planet2>
</SolarSystem>
Type Handling
const options = {
typeHandler: (v) => [true, v];
// return [<bool for creating tag>, <value>]
// runs for every nested property
}
Using Options
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 version="1.0"?>
<SolarSystem>
<Planet>Earth</Planet>
</SolarSystem>
1.3.0
9 months ago
1.2.8
2 years ago
1.2.7
2 years ago
1.2.6
2 years ago
1.2.5
2 years ago
1.2.4
2 years ago
1.2.3
2 years ago
1.2.2
2 years ago
1.2.1
4 years ago
1.2.0
4 years ago
1.1.0
5 years ago
1.0.9
5 years ago
1.0.8
5 years ago
1.0.7
5 years ago
1.0.6
5 years ago
1.0.5
5 years ago
1.0.2
5 years ago
1.0.4
5 years ago
1.0.3
5 years ago
1.0.1
5 years ago
1.0.0
5 years ago