xast utility to parse from XML
xast-util-from-xml is a utility within the unified ecosystem designed to parse serialized XML strings or `Uint8Array` into a `xast` (XML Abstract Syntax Tree) compliant syntax tree. Currently at version 4.0.0, the package maintains an active development status with major releases tied to significant internal changes and Node.js version updates. It leverages the `@rgrove/parse-xml` library for efficient and robust XML parsing, then transforms its output into the standardized `xast` format. This allows developers to programmatically interact with XML structures using a consistent AST representation, often for transformations or analysis. It is primarily differentiated by its integration into the broader `unified` and `xast` ecosystem, providing a predictable and well-typed interface for XML processing in JavaScript and TypeScript environments. The package is ESM-only and compatible with Node.js 16 and higher.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use CommonJS `require()` syntax in an ESM-only package or environment.fixReplace `const fromXml = require('xast-util-from-xml')` with `import { fromXml } from 'xast-util-from-xml'`. -
VFileMessage: Cannot parse XML (1:1): unknown:
cause The input XML string or buffer is malformed, invalid, or contains constructs not supported by the underlying `@rgrove/parse-xml` parser.fixExamine the XML input for syntax errors (e.g., unclosed tags, invalid characters, incorrect nesting, problematic entities) and correct them to ensure it is well-formed. -
SyntaxError: Cannot use import statement outside a module
cause Running an ESM-only package in a Node.js environment configured for CommonJS, or an older Node.js version (<16) that doesn't fully support ESM without specific configuration.fixEnsure your `package.json` includes `"type": "module"` for your project, or rename your file to `.mjs`. Also, verify you are running Node.js v16 or higher. -
TypeError: fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile is not a function (or similar for other Node.js built-ins in browser)
cause Attempting to run Node.js-specific modules (like `node:fs/promises`) within a browser environment without proper polyfills or build-time shimming.fixEnsure your build process (e.g., Webpack, Rollup) correctly handles Node.js module resolution for browser targets, or load XML content in the browser using browser-native APIs like `fetch` or direct string assignment.
Warnings
- breaking Version 4.0.0 and above require Node.js 16 or newer. Running on older Node.js versions will result in runtime errors related to unsupported syntax or module resolution.
- breaking Version 4.0.0 switched to using the `exports` field in `package.json` for module resolution. This means direct imports of internal or private API paths (e.g., `xast-util-from-xml/lib/file`) will break.
- breaking Version 3.0.0 replaced the internal `sax` parser with `@rgrove/parse-xml`. While providing improved parsing capabilities, this change can lead to more stringent error detection for malformed XML, potentially causing previously 'working' (but technically incorrect) XML to now throw parsing errors.
- breaking Version 2.0.0 transitioned the package to be ESM-only. CommonJS `require()` statements are no longer supported and will cause runtime errors.
- gotcha XML parsing, especially with user-provided data, can be a source of security vulnerabilities (e.g., XXE attacks, Billion Laughs attacks). The package does not inherently sanitize or protect against all XML-related exploits.
Install
-
npm install xast-util-from-xml -
yarn add xast-util-from-xml -
pnpm add xast-util-from-xml
Imports
- fromXml
const fromXml = require('xast-util-from-xml')import { fromXml } from 'xast-util-from-xml' - fromXml (browser/Deno)
import { fromXml } from 'https://esm.sh/xast-util-from-xml@4' - Root (xast type)
import type { Root } from 'xast'
Quickstart
import fs from 'node:fs/promises'
import {fromXml} from 'xast-util-from-xml'
async function parseExampleXml() {
// Assume example.xml contains:
// <album id="123">
// <name>Born in the U.S.A.</name>
// <artist>Bruce Springsteen</artist>
// <releasedate>1984-04-06</releasedate>
// </album>
// For demonstration, create a dummy file or use a string directly
const xmlContent = `<album id="123"><name>Born in the U.S.A.</name><artist>Bruce Springsteen</artist><releasedate>1984-04-06</releasedate></album>`;
const tree = fromXml(xmlContent);
// In a real scenario, you'd read from a file:
// const tree = fromXml(await fs.readFile('example.xml', 'utf8'))
console.dir(tree, {depth: undefined})
}
parseExampleXml().catch(console.error)