admin管理员组文章数量:1332383
I'm kinda new to node.js and really new to using it with XML. I can't figure out how to find a specific element in a XML document based on the element's ID and type.
Basically, what I'm trying to do is handle requests with Express and then using a value from a query string in the request, search for an element in a XML document and send the contents of that element back as the response.
Here's some of my code:
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);
Thanks in advance!
I'm kinda new to node.js and really new to using it with XML. I can't figure out how to find a specific element in a XML document based on the element's ID and type.
Basically, what I'm trying to do is handle requests with Express and then using a value from a query string in the request, search for an element in a XML document and send the contents of that element back as the response.
Here's some of my code:
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);
Thanks in advance!
Share Improve this question asked Nov 25, 2014 at 19:55 josjos 481 silver badge6 bronze badges2 Answers
Reset to default 6xmldom provides an implementation of the DOM Api for node.js
var DOMParser = new (require('xmldom')).DOMParser;
var document = DOMParser.parseFromString(xmlString);
var nodeById = document.getElementById('someId');
var nodesByName = document.getElementsByTagName('someName');
If you need more flexibility check out xpath.
You need to use some XML parser like libxmljs, xmldoc or node-xml2js which is a XML to JS object converter.
本文标签: javascriptFinding XML elements by type and ID in nodejsStack Overflow
版权声明:本文标题:javascript - Finding XML elements by type and ID in node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742298695a2449243.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论