[image: Inline image 1]

*Code->*

var fs = require('fs');
var sax = require('sax');

parser = sax.parser(true);

elements_array = [];
nodeCounter = -1;

parser.onerror = function(e) {
    // an error happened.
  };

parser.onopentag = function(node) {
    // opened a tag. node has "name" and "attributes"
    if (node.name == 'node') {
      nodeCounter++;
      elements_array.push({
      group: 'nodes',
      data: { id: node.attributes.id, label: node.attributes.label }
      });

    if (node.name == 'edge') {
      nodeCounter++;
      elements_array.push({
      group: 'edges',
      data: { label: node.attributes.label, source: node.attributes.source,
target: node.attributes.target }
      });

    if (node.name =='att') {
      var key = node.attributes.name;
      elements_array[nodeCounter].data.key = { type : node.attributes.type,
value: node.attributes.value};
    };
    };

  };

 parser.onend = function() {
    // parser stream is done, and ready to have more stuff written to it.
    console.log("XML has been parsed.\n");
  };


try {
    var file_buf = fs.readFileSync('./graph.xml');
    parser.write(file_buf.toString('utf8')).close();
} catch(ex) {
    // keep 'em silent
}

console.log(elements_array);

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

<<Screen Shot 2014-02-12 at 12.41.30 AM.png>>

Reply via email to