Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Updating the readme with usage and method/event documentation.

  • Loading branch information...
commit 7180f5e5107b6332d274335c16d7137ba025bce2 1 parent 65b8ce1
Randy Merrill Zoramite authored

Showing 1 changed file with 55 additions and 1 deletion. Show diff stats Hide diff stats

  1. +55 1 readme.md
56 readme.md
Source Rendered
... ... @@ -1,3 +1,57 @@
1 1 # Node.js XML 2 Object
2 2
3   -Simple wrapper on the SAX parser to parse xml into javascript objects.
  3 +Simple wrapper on the [SAX.js](https://github.com/isaacs/sax-js) parser to parse xml into javascript objects.
  4 +
  5 +Give `xml2object` a XML file and an array of XML elements and it will parse through the
  6 +
  7 +## Install
  8 +
  9 + npm install xml2object
  10 +
  11 +## Usage
  12 +
  13 + var xml2object = require('xml2object');
  14 +
  15 + // Create a new xml parser with an array of xml elements to look for
  16 + var parser = new xml2object('myAnimals.xml', [ 'animal' ]);
  17 +
  18 + // Bind to the object event to work with the objects found in the XML file
  19 + parser.on('object', function(name, obj) {
  20 + console.log('Found an object: %s', name);
  21 + console.log(obj);
  22 + });
  23 +
  24 + // Bind to the file end event to tell when the file is done being streamed
  25 + parser.on('end', function(name, obj) {
  26 + console.log('Finished parsing xml!');
  27 + });
  28 +
  29 + // Start parsing the XML
  30 + parser.start();
  31 +
  32 +## Module
  33 +
  34 +### xml2object(xmlFile, elements)
  35 +
  36 +Constructor for creating an instance of the xml parser
  37 +
  38 + var xml2object = require('xml2object');
  39 +
  40 + // Parse the myAnimals.xml file looking for <animal> elements
  41 + var parser = new xml2object('myAnimals.xml', [ 'animal' ]);
  42 +
  43 +### .start()
  44 +
  45 +Triggers the xml file to start streaming to the parser. Call this method after you have bound to the events.
  46 +
  47 +### Event: 'object'
  48 +
  49 + function(name, obj) { ... }
  50 +
  51 +Triggered when an object has been parsed from the XML file with the name of the element found and the actual object.
  52 +
  53 +### Event: 'end'
  54 +
  55 + function() { ... }
  56 +
  57 +Marks the end of the input file when it has been completely streamed through the parser.

0 comments on commit 7180f5e

Please sign in to comment.
Something went wrong with that request. Please try again.