node-xml-to-json
TypeScript icon, indicating that this package has built-in type declarations

1.2.3 • Public • Published

node-xml-to-json

Easily convert XML data to JS Object or JSON


NPM

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-json --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"
                }
        }
}

Package Sidebar

Install

npm i node-xml-to-json

Weekly Downloads

1

Version

1.2.3

License

ISC

Unpacked Size

18.9 kB

Total Files

7

Last publish

Collaborators

  • ashp116