Unlocking the Power of XML in Node.js Applications

Receiving XML over HTTP

In the past, XML-RPC and SOAP web services were the standard for exchanging data between applications. Although JSON APIs have become the norm, there are still cases where you might need to expose an XML-based API to allow other applications to feed XML data into your application.

To work with XML in Node.js, you can use the xml2js package, which pairs well with Express using the express-xml-bodyparser middleware. By default, the middleware will parse any incoming requests where the request’s Content-Type header is set to text/xml.

const express = require('express');
const xmlBodyParser = require('express-xml-bodyparser');

const app = express();
app.use(xmlBodyParser());

app.post('/api/endpoint', (req, res) => {
  const xmlDoc = req.body;
  // Process the XML document
  res.send('XML received successfully!');
});

Validating XML with Schemas

An XML schema (XSD) is often used to define the XML message structure that each application should expect to send and receive. The libxmljs2 package supports XML schema validation and allows you to load an XML schema from the server’s file system and use it to validate incoming XML data.

const libxmljs = require('libxmljs');

const xsd = libxmljs.parseXmlSchema(fs.readFileSync('schema.xsd', 'utf8'));
const xmlDoc = libxmljs.parseXml(xmlData);

xsd.validate(xmlDoc).then(() => {
  console.log('XML is valid!');
}).catch((err) => {
  console.error('XML is invalid:', err);
});

Manipulating HTML Content

What if your application needs to manipulate HTML? Unlike XML, HTML is not strictly compliant with the XML specification. Cheerio is a popular package that specializes in dealing with the nuances of HTML compared to XML.

const cheerio = require('cheerio');

const $ = cheerio.load('
Cell 1 Cell 2
');
const cells = $('td').map((i, el) => $(el).text()).get();
console.log(cells); // Output: ['Cell 1', 'Cell 2']

Generating SVG Images

Scalable Vector Graphics (SVG) is an XML-based image format supported by all major browsers. SVG XML is composed of a series of elements that define different types of shapes. CSS styles can be included with each shape to define its appearance.

The svgson package allows you to convert between SVG XML and JSON, making it easier to manipulate an SVG image with JavaScript.

const svgson = require('svgson');

const svgXml = fs.readFileSync('image.svg', 'utf8');
const svgJson = svgson.parse(svgXml);

// Modify the SVG image
svgJson.elements[0].attributes.fill = 'ed';

const newSvgXml = svgson.stringify(svgJson);
fs.writeFileSync('new_image.svg', newSvgXml);

These packages can help you unlock the power of XML in your Node.js applications, whether you’re working with XML-based data exchanges, SVG image manipulation, or more.

  • xml2js: for bidirectional conversion between XML and JavaScript
  • libxmljs2: for validating XML against schemas
  • Cheerio: for parsing and manipulating HTML
  • svgson: for generating SVG images

Leave a Reply