Why chose this module?
Node-xml-to-js module is easy and beginner-friendly module. It allows you to use isolated function for converting. The best part is you can customize: beautify your code, ignore root, ignore declaration, and more!
Installation
npm install node-xml-to-js --save
Example
Here is an example project
XML to JS Object
user.xml
<Ashp116 id="116">
<favirote>
<color>Red</color>
<sport>Tennis, Basket Ball</sport>
<gaming>None</gaming>
<hobby>Game Development</hobby>
</favirote>
<IsAMillionaire>false</IsAMillionaire>
<IsATrillionaire>true</IsATrillionaire>
<extra>
<joke>What's the best thing about Switzerland? I don't know, but the flag is a big plus</joke>
</extra>
</Ashp116>
index.ts
import {toObject, toJSON} from 'node-xml-to-js'
fs.readFile("path/to/user.xml", 'utf8' , (err, data) => {
if (err) {
console.error(err)
return
}
let User = parser.toObject(data)
console.log(User)
})
output
{
Ashp116: {
_id: '116',
favirote: {
color: 'Red',
sport: 'Tennis, Basket Ball',
gaming: 'None',
hobby: 'Game Development'
}
}
}
XML to JSON
user.xml
<Ashp116 id="116">
<favirote>
<color>Red</color>
<sport>Tennis, Basket Ball</sport>
<gaming>None</gaming>
<hobby>Game Development</hobby>
</favirote>
<IsAMillionaire>false</IsAMillionaire>
<IsATrillionaire>true</IsATrillionaire>
<extra>
<joke>What's the best thing about Switzerland? I don't know, but the flag is a big plus</joke>
</extra>
</Ashp116>
index.ts
import {toObject, toJSON} from 'node-xml-to-js'
fs.readFile("path/to/user.xml", 'utf8' , (err, data) => {
if (err) {
console.error(err)
return
}
let User = parser.toJSON(data, {beautify: true})
console.log(User)
})
output
{
"Ashp116": {
"_id": "116",
"favirote": {
"color": "Red",
"sport": "Tennis, Basket Ball",
"gaming": "None",
"hobby": "Game Development"
}
}
}