var sax = require('sax')
var parser = {}

parser.stream = function () {
  return sax.createStream({
    trim: true,
    normalize: true,
    lowercase: true
  })
}

parser.capture = function (stream) {
  return new Promise(function (resolve, reject) {
    var frames = [{}]

    stream.on('opentagstart', (node) => frames.push({}))
    stream.on('attribute', (node) => frames[frames.length - 1][node.name] = node.value)
    stream.on('text', (text) => text.trim().length && (frames[frames.length - 1] = text.trim()))
    stream.on('closetag', (name) => frames[frames.length - 2][name] = []
      .concat(frames[frames.length - 2][name] || [])
      .concat(frames.pop()))
    stream.on('end', () => resolve(frames.pop()))
  })
}

module.exports = parser

@@ -715,16 +715,47 @@
 pop()))%0A
+    stream.on('error', reject)%0A
   %7D)%0A%7D%0A%0A

Throw an error if the XML is invalid.