Skip to content

Instantly share code, notes, and snippets.

@lbdremy
Created October 4, 2011 16:14
Show Gist options
  • Save lbdremy/1262059 to your computer and use it in GitHub Desktop.
Save lbdremy/1262059 to your computer and use it in GitHub Desktop.
Test Sax. The `text` event is called two times. Why?
var sax = require('sax'),
fs = require('fs');
var strict = true;
var tag;
// Stream usage
// takes the same options as the parser
var saxStream = require("sax").createStream(strict)
saxStream.on("error", function (err) {
// unhandled errors will throw, since this is a proper node
// event emitter.
console.error("error", err)
// clear the error
this._parser.error = null;
this._parser.resume();
})
saxStream.on("opentag", function (node) {
tag = node.name;
})
saxStream.on("text", function (text) {
console.log(tag + ':' + text);
})
saxStream.on("end", function (node) {
console.log('end');
})
// pipe is supported, and it's readable/writable
// same chunks coming in also go out.
var XMLStream = fs.createReadStream( __dirname + "/test-pt.xml");
XMLStream.pipe(saxStream);
console.log('start');
<?xml version="1.0" encoding="utf-8" ?>
<products>
<product>
<lastUpdated>03/10/2011 17:34:57</lastUpdated>
<title>Titanium Gents Cubic Zirconia Earrings - Set of 2</title>
<sku>2126119</sku>
<ean></ean>
<url>http://www.jdoqocy.com/click-4187113-10697412?URL=http%3A%2F%2Fwww.argos.co.uk%2Fstatic%2FProduct%2FpartNumber%2F2126119%2Fc_1%2F1%7Ccategory_root%7CJewellery%2Band%2Bwatches%7C14416987%2Fc_2%2F2%7C14416987%7CEarrings%7C14417016.htm</url>
<imageUrl>http://www.argos.co.uk/wcsstore/argos/images/6-2126119MMA72UC589246M.jpg</imageUrl>
<price>12.99</price>
<priceWas></priceWas>
<priceValidUntil>20/01/2012</priceValidUntil>
<deliveryCost>5.95</deliveryCost>
<deliveryAvailable>yes</deliveryAvailable>
<deliveryDetails>Home delivery within 2 days</deliveryDetails>
<storePickupAvailable>yes</storePickupAvailable>
<storePickupDetails>Reserve it now</storePickupDetails>
<moneyBackExclusion>yes</moneyBackExclusion>
<buyingGuideName></buyingGuideName>
<buyingGuideUrl></buyingGuideUrl>
<creditOffer>no</creditOffer>
<new>no</new>
<internetOnly>no</internetOnly>
<brand></brand>
<colour></colour>
<style></style>
<collection></collection>
<description_product>2 individual for the fashion conscious male. Set of 2 individual earrings; Titanium; Cubic zirconia set; Maximum size in set 15mm.</description_product>
<keywords>Titanium, Gents, Cubic, Zirconia, Earrings, Set, Jewellery &amp; Watches, Men&apos;s jewellery, 2126119</keywords>
<overallCustomerRating>5</overallCustomerRating>
<numberOfReviews>2</numberOfReviews>
<currency>GBP</currency>
<categoryList>Jewellery &amp; Watches &gt; Men&apos;s jewellery; Jewellery &amp; Watches &gt; Earrings</categoryList>
<categories>
<category>
<category_department>Jewellery &amp; Watches</category_department>
<category_section>Men&apos;s jewellery</category_section>
<name></name>
</category>
<category>
<category_department>Jewellery &amp; Watches</category_department>
<category_section>Earrings</category_section>
<name></name>
</category>
</categories>
<features>
</features>
<extras>
</extras>
<offers>
</offers>
<accessories>
</accessories>
<alternatives>
</alternatives>
</product>
</products>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment