Sha256: 2154d5826b9acbfd9f30863eb040339c05b8abba8a54d8c4843bbc775ae0522d

Contents?: true

Size: 1.51 KB

Versions: 45

Compression:

Stored size: 1.51 KB

Contents

// pull out /GeneralSearchResponse/categories/category/items/product tags
// the rest we don't care about.

var sax = require("../lib/sax.js")
var fs = require("fs")
var path = require("path")
var xmlFile = path.resolve(__dirname, "shopping.xml")
var util = require("util")
var http = require("http")

fs.readFile(xmlFile, function (er, d) {
  http.createServer(function (req, res) {
    if (er) throw er
    var xmlstr = d.toString("utf8")

    var parser = sax.parser(true)
    var products = []
    var product = null
    var currentTag = null

    parser.onclosetag = function (tagName) {
      if (tagName === "product") {
        products.push(product)
        currentTag = product = null
        return
      }
      if (currentTag && currentTag.parent) {
        var p = currentTag.parent
        delete currentTag.parent
        currentTag = p
      }
    }

    parser.onopentag = function (tag) {
      if (tag.name !== "product" && !product) return
      if (tag.name === "product") {
        product = tag
      }
      tag.parent = currentTag
      tag.children = []
      tag.parent && tag.parent.children.push(tag)
      currentTag = tag
    }

    parser.ontext = function (text) {
      if (currentTag) currentTag.children.push(text)
    }

    parser.onend = function () {
      var out = util.inspect(products, false, 3, true)
      res.writeHead(200, {"content-type":"application/json"})
      res.end("{\"ok\":true}")
      // res.end(JSON.stringify(products))
    }

    parser.write(xmlstr).end()
  }).listen(1337)
})

Version data entries

45 entries across 45 versions & 3 rubygems

Version Path
ela-4.1.6 node_modules/sax/examples/get-products.js
ela-4.1.5 node_modules/sax/examples/get-products.js
ela-4.1.4 node_modules/sax/examples/get-products.js
ela-4.1.3 node_modules/sax/examples/get-products.js
ela-4.1.2 node_modules/sax/examples/get-products.js
ela-4.1.1 node_modules/sax/examples/get-products.js
ela-4.1.0 node_modules/sax/examples/get-products.js
ela-4.0.0 node_modules/sax/examples/get-products.js
ela-3.4.3 node_modules/sax/examples/get-products.js
ela-3.4.2 node_modules/sax/examples/get-products.js
ela-3.4.0 node_modules/sax/examples/get-products.js
ela-3.3.1 node_modules/sax/examples/get-products.js
ela-3.3.0 node_modules/sax/examples/get-products.js
ela-3.2.0 node_modules/sax/examples/get-products.js
ela-3.1.1 node_modules/sax/examples/get-products.js
ela-3.1.0 node_modules/sax/examples/get-products.js
ela-3.0.0 node_modules/sax/examples/get-products.js
ela-2.0.0 node_modules/sax/examples/get-products.js
ela-1.1.0 node_modules/sax/examples/get-products.js
stylus-source-0.54.5 vendor/node_modules/sax/examples/get-products.js