Read XML file from disk

458 views
Skip to first unread message

ravi prakash

unread,
Aug 3, 2012, 8:45:32 AM8/3/12
to libxmljs
Hi,

I was trying out various xml parsers in node js and found
libxmljs particularly interesting.
My goal is to read xml file directly from disk. Is there a method
to support such operation.

I found such option in another xml parser called xml2js. The method
is:

var parser = new xml2js.Parser();
fs.readFile(__dirname + '/foo.xml', function(err, data) {
parser.parseString(data, function (err, result) {

});
});

How can i achieve the same thing in libxmljs parser.


regards,
Ravi

Roman Shtylman

unread,
Sep 2, 2012, 5:43:04 PM9/2/12
to libx...@googlegroups.com
you would use the parseXmlString method and pass it the string data. The above should be re-written as:

 var libxmljs = require('libxmljs');

 fs.readFile(__dirname + '/foo.xml', function(err, data) { 
    var doc = libxmljs.parseXmlString(data.toString());

    // doc is now your parsed xml
 }); 

cheers,
~Roman
Reply all
Reply to author
Forward
0 new messages