---
title: "@std/xml"
description: "XML parsing and serialization for Deno."
jsr: jsr:@std/xml
pkg: xml
version: 0.1.0
generated: true
stability: stable
---
<!-- Autogenerated from JSR docs. Do not edit directly. -->

## Overview

<p>XML parsing and serialization for Deno.</p>
<p>This module implements a non-validating XML 1.0 parser based on the
<a href="https://www.w3.org/TR/xml/" rel="nofollow">W3C XML 1.0 (Fifth Edition)</a> specification.</p>
<h2 id="parsing-apis">
Parsing APIs</h2>
<p>Two parsing APIs are provided for different use cases:</p>
<table>
<thead>
<tr>
<th>API</th>
<th>Use Case</th>
<th>Output</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://jsr.io/@std/xml@0.1.0/doc/~/parse" rel="nofollow"><code>parse</code></a></td>
<td>Parse a complete XML string</td>
<td>Document tree</td>
</tr>
<tr>
<td><a href="https://jsr.io/@std/xml@0.1.0/doc/~/parseXmlStream" rel="nofollow"><code>parseXmlStream</code></a></td>
<td>Streaming with maximum throughput</td>
<td>Direct callbacks</td>
</tr>
</tbody>
</table>
<h2 id="quick-examples">
Quick Examples</h2>
<h3 id="dom-style-parsing">
DOM-style parsing</h3>

```js
import { parse } from "@std/xml";
import { assertEquals } from "@std/assert";

const doc = parse("<root><item>Hello</item></root>");
assertEquals(doc.root.name.local, "root");
```

<h3 id="high-performance-streaming-with-callbacks">
High-performance streaming with callbacks</h3>
<p>For maximum throughput when processing large files:</p>

```js
import { parseXmlStream } from "@std/xml";

const response = await fetch("https://example.com/feed.xml");
const textStream = response.body!.pipeThrough(new TextDecoderStream());

let itemCount = 0;
await parseXmlStream(textStream, {
  onStartElement(name) {
    if (name === "item") itemCount++;
  },
});
console.log(`Found ${itemCount} items`);
```

<h3 id="streaming-with-byte-streams">
Streaming with byte streams</h3>
<p>For convenience with fetch responses:</p>

```js
import { parseXmlStreamFromBytes } from "@std/xml";

const response = await fetch("https://example.com/feed.xml");

await parseXmlStreamFromBytes(response.body!, {
  onStartElement(name) {
    console.log(`Element: ${name}`);
  },
});
```

<h2 id="position-tracking">
Position Tracking</h2>
<p>Both parsers support optional position tracking (line, column, offset) for
debugging and error reporting:</p>
<ul>
<li>
<p><strong>DOM parser (<a href="https://jsr.io/@std/xml@0.1.0/doc/~/parse" rel="nofollow"><code>parse</code></a>)</strong>: Position tracking is <strong>enabled by default</strong>
to provide detailed error messages. Disable with <code>{ trackPosition: false }</code>
for a performance boost when parsing trusted XML.</p>
</li>
<li>
<p><strong>Streaming parser (<a href="https://jsr.io/@std/xml@0.1.0/doc/~/parseXmlStream" rel="nofollow"><code>parseXmlStream</code></a>)</strong>: Position tracking is
<strong>disabled by default</strong> for optimal streaming performance. Enable with
<code>{ trackPosition: true }</code> when you need position info.</p>
</li>
</ul>

### Add to your project

```sh
deno add jsr:@std/xml
```

<a href="https://jsr.io/@std/xml/doc" class="docs-cta jsr-cta">See all symbols in @std/xml on
<svg class="inline ml-1" viewBox="0 0 13 7" aria-hidden="true" height="20"><path d="M0,2h2v-2h7v1h4v4h-2v2h-7v-1h-4" fill="#083344"></path><g fill="#f7df1e"><path d="M1,3h1v1h1v-3h1v4h-3"></path><path d="M5,1h3v1h-2v1h2v3h-3v-1h2v-1h-2"></path><path d="M9,2h3v2h-1v-1h-1v3h-1"></path></g></svg></a>

<!-- custom:start -->
<!-- Add persistent custom content below. This section is preserved across generations. -->

<!-- custom:end -->
