在Node.js中读取XML文件

我正在学习如何使用节点。 在这个时候,我有一个XML文件,看起来像这样:

sitemap.xml的

<?xml version="1.0" encoding="utf-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> <url> <loc>http://www.example.com</loc> <lastmod>2015-10-01</lastmod> <changefreq>monthly</changefreq> </url> <url> <loc>http://www.example.com/about</loc> <lastmod>2015-10-01</lastmod> <changefreq>never</changefreq> </url> <url> <loc>http://www.example.com/articles/tips-and-tricks</loc> <lastmod>2015-10-01</lastmod> <changefreq>never</changefreq> <article:title>Tips and Tricks</blog:title> <article:description>Learn some of the tips-and-tricks of the trade</article:description> </url> </urlset> 

我正尝试在Node应用程序中加载这个XML。 加载时,我只想得到包含使用<article: elements <article:url元素。 在这个时候,我卡住了。 现在,我通过以下方式使用XML2JS :

 var parser = new xml2js.Parser(); fs.readFile(__dirname + '/../public/sitemap.xml', function(err, data) { if (!err) { console.log(JSON.stringify(data)); } }); 

console.log语句被执行时,我只是在控制台窗口中看到一堆数字。 像这样的东西:

 {"type":"Buffer","data":[60,63,120, ...]} 

我错过了什么?

使用xml2json

https://www.npmjs.com/package/xml2json

 fs = require('fs'); var parser = require('xml2json'); fs.readFile( './data.xml', function(err, data) { var json = parser.toJson(data); console.log("to json ->", json); }); 

从文档 。

callback传递两个参数(err,data),其中data是文件的内容。

如果没有指定编码,则返回原始缓冲区。

如果options是一个string,那么它指定编码。 例:

 fs.readFile('/etc/passwd', 'utf8', callback); 

你没有指定编码,所以你得到原始缓冲区。

fs.readFile有一个可选的第二个参数:编码。 如果你不包含这个参数,它会自动返回一个Buffer对象。

https://nodejs.org/api/fs.html#fs_fs_readfile_filename_options_callback

如果你知道编码只是使用:

 var parser = new xml2js.Parser(); fs.readFile(__dirname + '/../public/sitemap.xml', 'utf8', function(err, data) { if (!err) { console.log(data); } }); 

为了读取Node中的XML文件 ,我喜欢XML2JS包 。 这个包让我可以轻松地用JavaScript处理XML。

 var parser = new xml2js.Parser(); parser.parseString(fileData.substring(0, fileData.length), function (err, result) { var json = JSON.stringify(result); }); 

迟到这个线程,只是在这里添加一个简单的提示,如果你打算在js中使用parsing的数据或保存为json文件,一定要将explicitArray设置为false 。 输出将更加js友好

所以它会看起来像,
letparser=newxml2js.Parser({explicitArray:false})

参考: https : //github.com/Leonidas-from-XIV/node-xml2js

为什么没有人提到libxmljs包? 我只是读了一下,看起来很容易使用它来parsingxml。

您也可以在parsing之前使用regex删除不符合您的条件的元素:

 var parser = new xml2js.Parser(); fs.readFile(__dirname + '/../public/sitemap.xml', "utf8",function(err, data) { // handle err... var re = new RegExp("<url>(?:(?!<article)[\\s\\S])*</url>", "gmi") data = data.replace(re, ""); // remove node not containing article node console.log(data); //... parse data ... }); 

例如:

  var str = "<data><url><hello>abc</hello><moto>abc</moto></url><url><hello>bcd</hello></url><url><hello>efd</hello><moto>poi</moto></url></data>"; var re = new RegExp("<url>(?:(?!<moto>)[\\s\\S])*</url>", "gmi") str = str.replace(re, "") // "<data><url><hello>abc</hello><moto>abc</moto></url><url><hello>efd</hello><moto>poi</moto></url></data>"