# @xmpp/xml

> XMPP XML for JavaScript

Latest version **0.14.0** (published 2025-10-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install @xmpp/xml
pnpm add @xmpp/xml
yarn add @xmpp/xml
bun add @xmpp/xml
```

## Health

**Score 58/100 (C)** — status: stable.

Positive: has types package; esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.14.0 |
| Published | 2025-10-29 |
| First published | 2016-10-16 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | separate (@types/xmpp__xml) |
| Module format | ESM + CommonJS |
| Node | >= 20.10 |
| Dependencies | 2 |
| Unpacked size | 10.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2275 |
| Maintainers | sonny |
| Keywords | XMPP, stanza, iq, message, presence |

## Links

- npm: https://www.npmjs.com/package/@xmpp/xml
- Repository: https://github.com/xmppjs/xmpp.js
- Homepage: https://github.com/xmppjs/xmpp.js/tree/main/packages/xml
- Issues: http://github.com/xmppjs/xmpp.js/issues
- npm.io page: https://npm.io/package/@xmpp/xml

## Dependencies (2)

- [ltx](https://npm.io/package/ltx.md) ^3.1.2
- [@xmpp/events](https://npm.io/package/@xmpp/events.md) ^0.14.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.14.0 (latest) — 2025-10-29
- 0.13.3 — 2024-12-23
- 0.13.1 — 2022-02-13
- 0.13.0 — 2021-08-28
- 0.12.1 — 2021-08-22
- 0.12.0 — 2020-12-21
- 0.11.0 — 2020-02-14
- 0.10.0 — 2020-02-07
- 0.9.2 — 2020-01-02
- 0.9.1 — 2019-11-23
- 0.9.0 — 2019-11-19
- 0.8.0 — 2019-10-06
- 0.7.4 — 2019-06-15
- 0.7.0 — 2019-02-03
- 0.6.2 — 2019-01-07
- … 10 more at https://npm.io/package/@xmpp/xml/versions

## README

# xml

## Install

Note, if you're using `@xmpp/client` or `@xmpp/component`, you don't need to install `@xmpp/xml`.

`npm install @xmpp/xml`

```js
import xml from "@xmpp/xml";
import { xml } from "@xmpp/client";
import { xml } from "@xmpp/component";
```

## Writing

There's 2 methods for writing XML with xmpp.js

### factory

```js
import xml from "@xmpp/xml";

const recipient = "user@example.com";
const days = ["Monday", "Tuesday", "Wednesday"];
const message = xml(
  "message",
  { to: recipient },
  xml("body", {}, 1 + 2),
  xml(
    "days",
    {},
    days.map((day, idx) => xml("day", { idx }, day)),
  ),
);
```

If the second argument passed to `xml` is a `string` instead of an `object`, it will be set as the `xmlns` attribute.

```js
// both are equivalent
xml("time", "urn:xmpp:time");
xml("time", { xmlns: "urn:xmpp:time" });
```

### JSX

```js
/** @jsx xml */

import xml from "@xmpp/xml";

const recipient = "user@example.com";
const days = ["Monday", "Tuesday"];
const message = (
  <message to={recipient}>
    <body>{1 + 2}</body>
    <days>
      {days.map((day, idx) => (
        <day idx={idx}>${day}</day>
      ))}
    </days>
  </message>
);
```

Requires a preprocessor such as [TypeScript](https://www.typescriptlang.org/) or [Babel](http://babeljs.io/) with [@babel/plugin-transform-react-jsx](https://babeljs.io/docs/en/next/babel-plugin-transform-react-jsx.html).

## Reading

### attributes

The `attrs` property is an object that holds xml attributes of the element.

```js
message.attrs.to; // user@example.com
```

### text

Returns the text value of an element

```js
message.getChild("body").text(); // '3'
```

### getChild

Get child element by name.

```js
message.getChild("body").toString(); // '<body>3</body>'
```

### getChildText

Get child element text value.

```js
message.getChildText("body"); // '3'
```

### getChildren

Get children elements by name.

```js
message.getChild("days").getChildren("day"); // [...]
```

Since `getChildren` returns an array, you can use JavaScript array methods such as [filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) and [find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) to build more complex queries.

```js
const days = message.getChild("days").getChildren("day");

// Find Monday element
days.find((day) => day.text() === "Monday");
days.find((day) => day.attrs.idx === 0);

// Find all days after Tuesday
days.filter((day) => day.attrs.idx > 2);
```

### parent

You can get the parent node using the parent property.

```js
console.log(message.getChild("days").parent === message);
```

### root

You can get the root node using the root method.

```js
console.log(message.getChild("days").root() === message);
```

## Editing

### attributes

The `attrs` property is an object that holds xml attributes of the element.

```js
message.attrs.type = "chat";
```

### text

Set the text value of an element

```js
message.getChild("body").text("Hello world");
```

### append

Adds text or element nodes to the last position.
Returns the parent.

```js
message.append(xml("foo"));
message.append("bar");
message.append(days.map((day) => xml("day", {}, day)));
// <message>
//   ...
//   <foo/>
//   bar
//   <day>Monday</day>
//   <day>Tuesday</day>
// </message>
```

### prepend

Adds text or element nodes to the first position.
Returns the parent.

```js
message.prepend(xml("foo"));
message.prepend("bar");
message.prepend(days.map((day) => xml("day", {}, day)));
// <message>
//   <day>Tuesday</day>
//   <day>Monday</day>
//   bar
//   <foo/>
//   ...
// </message>
```

### remove

Removes a child element.

```js
const body = message.getChild("body");
message.remove(body);
```

## JSON

You can embed JSON anywhere but it is recommended to use appropriate semantic.

```js
/** @jsx xml */

// write
message.append(
  <myevent xmlns="xmpp:example.org">
    <json xmlns="urn:xmpp:json:0">{JSON.stringify(days)}</json>
  </myevent>,
);

// read
JSON.parse(
  message
    .getChild("myevent", "xmpp:example.org")
    .getChildText("json", "urn:xmpp:json:0"),
);
```

See also [JSON Containers](https://xmpp.org/extensions/xep-0335.html) and [Simple JSON Messaging](https://xmpp.org/extensions/xep-0432.html).

## Parsing XML strings

`@xmpp/xml` include a function to parse XML strings.

⚠ Use with care. Untrusted input or substitutions can result in invalid XML and side effects.

```js
import { escapeXML, escapeXMLText } from "@xmpp/xml";
import parse from "@xmpp/xml/lib/parse.js";

const ctx = parse("<message><body>hello world</body></message>");
ctx.getChildText("body"); // hello world
```

If you must use with untrusted input, escape it with `escapeXML` and `escapeXMLText`.

```js
const message = parse(`
  <message to="${escapeXML(to)}">
    <body>${escapeXMLText(body)}</body>
  </message>,
`);
```

---
_Source: https://npm.io/package/@xmpp/xml · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
