New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

node-xml-to-json

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-xml-to-json

latest
Source
npmnpm
Version
1.2.3
Version published
Maintainers
1
Created
Source

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

Keywords

json

FAQs

Package last updated on 08 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts