2

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!

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use some XML parser like libxmljs, xmldoc or node-xml2js which is a XML to JS object converter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.