【发布时间】:2019-04-09 06:44:07
【问题描述】:
我有一个包含 10 个学生数据的 xml 文件。每个学生详细信息都以学生标签为界。如何使用 nodejs 从 xml 文件中检索单个数据?
【问题讨论】:
-
你解决了吗?
-
yes@LucasHendren
我有一个包含 10 个学生数据的 xml 文件。每个学生详细信息都以学生标签为界。如何使用 nodejs 从 xml 文件中检索单个数据?
【问题讨论】:
你有两个选择。您可以使用内置 fs 库来读取文件。如果未指定编码,您将获得可以读取的原始缓冲区。
https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback
此外,为了进一步提供帮助,您可以使用 xml2js 库。 示例
const xml2js = require('xml2js');
const fs = require('fs');
const parser = new xml2js.Parser({ attrkey: "ATTR" });
// this example reads the file synchronously
// you can read it asynchronously also
let xml_string = fs.readFileSync("data.xml", "utf8");
parser.parseString(xml_string, function(error, result) {
if(error === null) {
console.log(result);
}
else {
console.log(error);
}
});
https://usefulangle.com/post/106/nodejs-read-xml
然后您可以根据标签解析出您希望如何使用下载的数据。我会看看拆分功能
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
如果有帮助请告诉我
【讨论】: