我正在尝试使用node.js模块xml2js
我的代码很简单:
function testparse(pathname, callback) { var parser = require('xml2js').Parser(), util = require('util'), fs = require('fs'), fs.readFile(pathname, function (err, data) { parser.parseString(data, function(err, result) { console.log('Complete result:'); console.log(util.inspect(result, {depth: null})); //Work console.log('Try to access element:'); console.log(result.smil.body); //Work console.log(result.smil.body.update); //Undefined }); }); }
我的xml文件是:
<?xml version="1.0"?> <smil> <head/> <body> <update /*some field*//> <stream name="name"/> <playlist /*some field*/> <video /*some field*//> <video /*some field*//> <video /*some field*//> </playlist> </body> </smil>
输出给我:
Complete result: { smil: { head: [''], body: [ { update: [[Object]], stream: [[Object]], playlist: [[Object]] } ] } } Try to access element: [Object] Undefined
我已经通过尝试成功访问了正文,但是现在我陷入了困境,是否有xml2js如何在某个地方输出已解析的xml的模板或示例?
对于那些想知道的人,xml2js使用和滥用数组
对于我的文件,树将是:
.result //Object |_.head //Array |_.body //Array |_.update //Array | |_.$ //Object | |_.fields //Strings | |_.stream //Array | |_.$ //Object | |_.fields //Strings | |_.playlist //Array |_.$ //Object |_.fields //Strings | |_.video //Array |_.$ //Object |_.fields //Strings