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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

xmldom 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