Skip to content

通过Nodejs读取xml文件数据

发表于:

文章分类:web

文章标签:javascriptNodejsxmlsitemap.xmlxml2jsnodemon

阅读量:

博客是通过hexo搭建的,发布到线上也就是html静态文件了,总不能每次发版更新一个json文件吧,然后想到sitemapx.xml文件来代替json文件,因为每次hexo发布代码都需要更新sitemapx.xml文件。所以就想到用Nodejs读取xml文件。

示例

目录树:

bash
.
├── README.md
├── app.js
├── data
│   └── post-sitemap.xml
├── node_modules
│   ├── ...
├── package-lock.json
└── package.json

app.js

js
const fs = require('fs');
const xml2js = require('xml2js');
const parser = xml2js.Parser();

fs.readFile('./data/post-sitemap.xml', function (err, data) {
    parser.parseString(data, function (err, res) { 
        console.dir(res);
        console.log(res.urlset.url);
    });
})

代码仓库

https://github.com/Keystion/nodejs-read-xml-example

依赖

上一篇
下一篇
总访问量:-