XML Parser, Stringifier, and DOM
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
-
TypeError: xml.parse is not a function
cause Attempting to call `parse` on an undefined `xml` object, often due to incorrect `require` path or environment issues, or trying to use ES Modules `import` syntax.fixEnsure `const xml = require('xml-parse');` is correctly placed and the module is accessible. Verify you are not using ES Module `import` syntax. -
TypeError: Cannot read properties of undefined (reading 'tagName')
cause Attempting to access properties like `tagName` directly on the result of `xml.parse()` without realizing it returns an array (e.g., `parsedXML.tagName` instead of `parsedXML[0]?.tagName`).fixAccess elements from the parsed array, such as `parsedXML[0]` for the first root element, or iterate through the array to process multiple roots.
Warnings
- breaking The package is abandoned. It was last published over 6 years ago (version 0.4.0) and is not maintained. Consider using actively maintained alternatives like `fast-xml-parser` or `@rgrove/parse-xml` for modern projects.
- gotcha The `xml.parse` method always returns an array of nodes, even for valid XML documents with a single root. This is done to accommodate invalid XML with multiple root elements.
- gotcha The package is CommonJS-only and relies on `require()`. It does not officially support ES Modules (`import`) syntax, which is standard in modern JavaScript environments.
Install
-
npm install xml-parse -
yarn add xml-parse -
pnpm add xml-parse
Imports
- xml
import xml from 'xml-parse'
const xml = require('xml-parse') - parse
import { parse } from 'xml-parse'const xml = require('xml-parse'); xml.parse(xmlString) - stringify
import { stringify } from 'xml-parse'const xml = require('xml-parse'); xml.stringify(parsedXml, 2) - DOM
import { DOM } from 'xml-parse'const xml = require('xml-parse'); new xml.DOM(parsedXml)
Quickstart
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);