在 node.js 中按类型和 ID 查找 XML 元素

jos*_*jos 2 javascript xml node.js express

我对 node.js 有点陌生,对将它与 XML 一起使用非常陌生。我不知道如何根据元素的 ID 和类型在 XML 文档中查找特定元素。

基本上,我要做的是使用 Express 处理请求,然后使用请求中查询字符串中的值,在 XML 文档中搜索元素并将该元素的内容作为响应发送回。

这是我的一些代码:

var express = require('express'),
    xmlDoc = require('document.xml'),
    app = express();

app.get('/', function(req, res){
    var id = req.query.id;
    /* Here's where I want to get the element from xmlDoc based on id and element type*/
    res.send(/* Send the contents of the element */)
});

app.listen(3000);
Run Code Online (Sandbox Code Playgroud)

提前致谢!

ThW*_*ThW 5

xmldom为 node.js 提供了 DOM Api 的实现

var DOMParser = new (require('xmldom')).DOMParser;
var document = DOMParser.parseFromString(xmlString);

var nodeById = document.getElementById('someId');
var nodesByName = document.getElementsByTagName('someName');
Run Code Online (Sandbox Code Playgroud)

如果您需要更大的灵活性,请查看xpath