XML Parser, Stringifier, and DOM

0.4.0 · abandoned · verified Tue Apr 21

The `xml-parse` library, currently at version 0.4.0 and last published over 6 years ago, provides a tolerant parser, stringifier, and a simplified Document Object Model (DOM) for XML and HTML. Its key differentiator is its ability to handle malformed or invalid XML gracefully by always returning an array of root elements, even if there's only one. The library's components (parser, stringifier, and DOM) are designed to be lightweight and modular. It exclusively uses CommonJS `require` syntax, with no explicit mention or support for modern ES Modules. Given its low version number and the lack of recent updates, the package is no longer actively maintained and should be considered abandoned.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates parsing a valid XML string, accessing its structure via the simplified DOM, and then stringifying it back, showing the core functionalities.

const xml = require("xml-parse");

// 1. Parse an XML string (can handle invalid XML too)
const xmlString = '<?xml version="1.0" encoding="UTF-8"?><root><item>Hello</item><item>World</item></root>';
const parsedXML = xml.parse(xmlString);
console.log('Parsed XML (raw object structure):', JSON.stringify(parsedXML, null, 2));

// 2. Access elements using the simplified DOM
const xmlDoc = new xml.DOM(parsedXML);

// The root is always an array, so access the first element if expecting a single root
const rootElement = xmlDoc.document.childNodes[1]; // Skip the XML declaration

if (rootElement && rootElement.tagName === 'root') {
  console.log('\nRoot element tagName:', rootElement.tagName);
  const items = rootElement.getElementsByTagName('item');
  items.forEach((item, index) => {
    console.log(`Item ${index + 1} text:`, item.childNodes[0].text);
  });
} else {
  console.log('\nCould not find expected root element.');
}

// 3. Stringify the parsed object structure back to an XML string
const stringifiedXML = xml.stringify(parsedXML, 2); // 2 spaces for indentation
console.log('\nStringified XML:\n', stringifiedXML);

view raw JSON →