8

I'm looking around and not seeing an obvious way to parse XML in Node. I'm assuming that there's some relatively straight forward XML object that I can pass a String or url to, but I'm not finding anything like that in the spec. Do I need an external lib? and if so, which one would you guys recommend? I don't need xPath (though I wouldn't mind it) as long as I can walk the tree in an obvious way (test if nodeType == ElementNode and loop through children).

1
  • Off topic for opinion-seeking "what's the simplest?" and asking for recommendations ("which one would you guys recommend?"). Commented Feb 28, 2024 at 18:57

4 Answers 4

14

I suggest xml2js, a simple XML to JavaScript object converter. You can then iterate the resulting object. Code snippet from the page :

var parseString = require('xml2js').parseString;
var xml = "<root>Hello xml2js!</root>"
parseString(xml, function (err, result) {
    console.dir(result);
});

You can install it by using npm install xml2js

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

Comments

3

There are a variety of XML modules listed here where perhaps you may find one that works best for you. A popular one that I'm aware of is sax-js. There are also libxml bindings if you're already familiar with that library.

1 Comment

first link is dead.
1

If you're using Express along with body parser, the simplest way would be to use express-xml-bodyparser. It allows to parse XML for all routes or specific ones, depending on your scenario

All routes:

const xmlparser = require('express-xml-bodyparser');
// .. other middleware ... 
app.use(express.json());
app.use(express.urlencoded());
app.use(xmlparser());
// ... other middleware ... 

app.post('/receive-xml', function(req, res, next) {

  // req.body contains parsed XML as JSON

});

Specific routes:

app.post('/receive-xml', xmlparser({trim: false, explicitArray: false}), function(req, res, next) {
  // req.body contains parsed XML as JSON  
});

Comments

-4

If it isn't an absolute requirement that it has to be done server side, then on the client you could use XMLSerializer to parse into string, and DOMParser to parse into DOM.

XMLSerializer Example (from DOM to string):

var XMLS = new XMLSerializer();
var elem = document;
var xml_str = XMLS.serializeToString(elem);
console.log(xml_str);

DOMParser Example (from string to DOM):

var parser = new DOMParser();
var doc = parser.parseFromString(xml_str, "application/xml");
console.log(doc);

3 Comments

I don't have a "client side" in my environment. This is actually to parse a response that comes back to a cron process from a third party provider in the form of XML. But thanks. :)
this shouldn't have been downvoted given the original question. Works pretty well.
This question is specific to nodejs so I will just let anyone else that comes across this that node does not have a DomParser it's a browser thing.

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.