xast utility to parse from XML

4.0.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates parsing a string of XML content into a xast syntax tree and printing the resulting structure to the console.

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)

view raw JSON →