2.0.0 • Published 3 years ago

simple-xml-reader v2.0.0

Weekly downloads
12
License
ISC
Repository
github
Last release
3 years ago

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)
  }))
...
2.0.0

3 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago