javascript - 如何让 NodeJS Transform 流为每个输入 block 发出多个记录?

标签 javascript node.js stream transform-stream

我正在编写一个 Transform 流,它接受传入的 XML 流并将其子集作为 JS 对象发出。

<item>
  <name>some name</name>
  <files>
    <file path='path/to/file'/>
    <file path='path/to//other/file'/>
  </files>
</item>

需要编写两个item对象

{
  name: 'some name',
  file: 'path/to/file
}

{
  name: 'some name',
  file: 'path/to/other/file
}

因为它随后将通过管道传输到 CSV 编写器中。

代码看起来像

import fs from 'fs'
import expat from 'node-expat'
import { Transform } from 'stream'

const xParser = (filePath) => {
  const stream = fs.createReadStream(filePath)
  const parser = new expat.Parser('UTF-8')
  const tranny = Transform({ objectMode: true })

  tranny._transform = function transform(data, encoding, done) {
    this.push(data)
    done()
  }

  let current
  let inItem = false
  let tag
  let attributes

  const startTag = (name, attrs) => {
    if (name === 'item') {
      inItem = true
      current = {
        name: '',
        files: []
      }
      return
    }
    if (!inItem) return
    tag = name
    attributes = attrs
    if (tag === 'file') {
      current.files.push(attributes.path)
    }
  }

  const write = (file) => {
    tranny.write({
      name: current.name,
      file
    })
  }

  const endTag = (name) => {
    if (name === 'item') {
      // console.log('end item', JSON.stringify(current))
      inItem = false
      if (current.files.length === 0) {
        write('')
      } else {
        current.files.forEach(file => {
          write(file)
        })
      }
      tag = undefined
      attributes = undefined
    }
  }

  const handleText = (text) => {
    if (!inItem) return
    if (tag === 'name') {
      current.name = current.name.concat(text).trim()
    }
  }

  const handleDone = () => {
    console.log('done')
    tranny.end()
  }

  parser
  .on('startElement', startTag)
  .on('endElement', endTag)
  .on('text', handleText)
  .on('end', handleDone)

  stream.pipe(parser)
  return tranny
}

module.exports = xParser

当 xml 很简单时

<item>
  <name>some name</name>
  <files>
    <file path='path/to/file'/>
  </files>
</item>

这工作正常,但是当它达到 item 时如果有多个归档,它就会停止,'end'第二次 tranny.write 时立即触发发生了。

如何让 Transform 流为每个输入 block 发出多个记录?

最佳答案

好的,已修复。

这个解决方案是不执行多个 tranny.write ,而是在 tranny._transform 函数内进行数据转换(这应该是显而易见的)。

很喜欢

tranny._transform = function transform(data, encoding, done) {
  if (data.files.length > 0) {
    data.files.forEach((file) => {
      this.push({
        name: data.name,
        file
      })
    })
  } else {
    this.push({
      name: data.name,
      file: ''
    })
  }
  done()
}

endTag 处理程序现在看起来像

const endTag = (name) => {
  if (name === 'item') {
    inItem = false
    tranny.write(current)
    tag = undefined
    attributes = undefined
  }
}

这意味着我也可以删除 write 函数。

现在它可以工作了,并且当我这样做时进行了验证

import xParser from './xParser'
import concat from 'concat-stream'

xmlParser('data/mybigdatafile.xml')
.pipe(concat((data) => {
  console.log(JSON.stringify(data));
}))

这为我提供了完整的数据集。

关于javascript - 如何让 NodeJS Transform 流为每个输入 block 发出多个记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44804808/

相关文章:

javascript - 如何动态更改当前菜单元素AngularJS的标题(指令中的模板)

javascript - 如何解决 Heroku 上找不到模块的问题?

node.js - Node JS 流 : Understanding data concatenation

c++ - 当数据有空格时,使用 C++ 的流运算符 >> 读取格式化数据

javascript - 当我第二次调用 getJSON 时,它不是一个函数

javascript - 当 View 以模型默认值声明时,不会触发事件

c# - 未处理的异常 - 0x800a1391 - JavaScript 运行时错误 : 'SelectAllCheckBoxes' is undefined

javascript - 请求nodejs中的数据二进制

node.js - 在Windows中从.csv文件将数据导入mongodb数据库

swift utf16 数据流 - 分成 block 的问题