Socket
Socket
Sign inDemoInstall

simple-xml-reader

Package Overview
Dependencies
16
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    simple-xml-reader

Utility for load xml files with stream


Version published
Weekly downloads
29
decreased by-3.33%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

simple-xml-reader

A utility to read xml files through NodeJS Stream.

Build Status

Instalation

npm i simple-xml-reader --save

Usage

const { Writable } = require('stream')
const fs = require('fs')
const { xmlParser } = require('simple-xml-reader')

const write = (fn) => {
  return new Writable({
    objectMode: true,
    write (chunk, _, callback) {
      fn(chunk)
      callback()
    }
  })
}

fs.createReadStream('path to xml file'))
  .pipe(xmlParser())
  .pipe(write((node) => {
    console.log('XML Node', node)
  }))

Caution

The each xml tag readed, the transform stream will generate a JavaScript object to represent it. The created object has the following structure:

{
    "parent": {...},
    "name": "node name",
    "attrs": { node attributes },
    "text": { node text },
    "children": [{ children nodes } ...]
  }

If you have a very large XML file with many nodes and do not need the relationship between them, you can modify the object created after creation to remove the relationship between the child and parent nodes. This strategy is useful for avoiding excessive memory consumption. See the example below that removes the links between the nodes:

...
fs.createReadStream('path to xml file'))
  .pipe(xmlParser({
      mapElement: (e) => {
        e.parent = null // remove the link with parent node
        e.children = [] // remove the links with children nodes
        return e
      }
  }))
  .pipe(write((node) => {
    console.log('XML Node', node)
  }))
...

Keywords

FAQs

Last updated on 18 Jul 2019

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc