Node - Load an Xml File with xmldom

  • Estimated read time: 3 min read
  • Written by Chad Campbell on Aug 26th 2013

Often times you'll find yourself needing to load a serialized version of some data. Sometimes that data may be represented as XML. In those cases where you're working in a Node environment, you can use the xmldom package.

XMLDOM is a pure JavaScript W3C standards-based DOMParser and XMLSerializer. To install this tool, you can use the NodeJS package manager to install the latest version of the xmldom package with the following command.

npm install xmldom

Once installed, you can parse an XML file with the following code:

var fs = require('fs');
var parser = require('xmldom').DOMParser;

var filePath = GetFilePath();   // Assume this returns a fully qualified XML file path
try {
    var fileData = fs.readFileSync(filePath, 'ascii');
    var doc = new parser().parseFromString(fileData.substring(2, fileData.length));
    console.log("File '" + filePath + "/ was successfully read.");
} catch (ex) {
    console.log("Unable to read file '" + filePath + "'.");

The above snippet works as follows:

  • Line 1 - The Node.js File System module provides wrappers to work with File I/O. We need these wrappers to access the .xml file on the local file system.
  • Line 2 - The DOMParser from the xmldom module is loaded and aliased for later use in line 7.
  • Line 6 - The contents of the .xml file is read from the file system synchronously. In this example, we are specifying that ASCII encoding should be used.
  • Line 7 - This is where the contents of the file are loaded as XML.

This small bit of code empowers you to open XML files in Node. In the event that the XML is invalid, you will receive an error that says something like "end tag name: teams is not match the current start tagName:undefined". You will also receive this error if the contents of your XML file do not include the XML declaration mentioned here.

It is also important to note that as of Node v0.10.17 and xmldom 0.1.16, this example will only work if the xmldom package is installed locally. A global installation of xmldom will not work. If the package is not installed locally, you will receive an error that says something to the tune of "Error: Cannot find module 'xmldom'".

With the notes above, you should be able to successfully open an .xml file in Node. If you have any questions or comments related to this post, please leave them in the comments below. If you would like other help from Ecofic, please Contact Us.


comments powered by Disqus

Chad Campbell
Chad Campbell

Chad is an independent software professional. He has been named a Microsoft MVP five times. His books have been translated into multiple languages and distributed worldwide. He holds a computer science degree from Purdue University, where he also studied psychology.

Chad has built sites, apps, frameworks, libraries, and platforms using Java, .NET, and Node. He's ran his own startups and has created software for Fortune 100 companies. In short, Chad knows how to create software. From ideation to delivery. From start-to-finish.

Follow Chad Online