npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

xml2o

v0.4.14

Published

Helps you convert XML into an object for easy reading.

Readme

xml2o

Convert XML into lightweight, queryable JavaScript objects without a DOM.

Installation

Install xml2o with your package manager:

npm install xml2o
bun add xml2o
yarn add xml2o

Usage

convertString and convertStream parse asynchronously and return a Promise<Node>.

ESM

import { convertString } from "xml2o";

const root = await convertString('<root><item id="1">value</item></root>');
console.log(root.query("item")[0]?.text); // value

CommonJS

const { convertString } = require("xml2o");

async function readXml() {
    const root = await convertString('<root><item id="1">value</item></root>');
    console.log(root.query("item")[0]?.getAttribute("id")); // 1
}

readXml();

Streams

Pass a Node.js readable stream to convertStream:

import { createReadStream } from "node:fs";
import { convertStream } from "xml2o";

const root = await convertStream(createReadStream("/path/to/file.xml"));

Invalid XML rejects the returned promise, so handle conversion errors with try/catch or .catch():

try {
    await convertString("<root><item></root>");
} catch (error) {
    console.error("Could not parse XML", error);
}

API

convertString(xml)

Parses an XML string and resolves to the root Node.

convertStream(stream)

Parses a readable stream and resolves to the root Node.

Node

A Node is an array of its child nodes. It exposes the element's name, local name, prefix, namespace uri, parent, and root. Its text property concatenates text and CDATA received for the node and its direct child elements.

Use attribute helpers to read attributes:

const item = root.query("item")[0];

item?.getAttribute("id"); // "1"
item?.hasAttribute("id"); // true
item?.getAttributeNode("id"); // Attribute | undefined
item?.getAttributes(); // { id: "1" }

getAttribute, getAttributeNode, and hasAttribute accept an optional namespace URI as their second argument. getAttributes(uri) returns attributes in that namespace; without an argument it returns non-namespaced attributes.

Use query(path, uri?) to find child elements by their local name. Paths have these forms:

| Path | Meaning | | --------------- | ----------------------------------------------------- | | "item" | Find every descendant item node. | | "group/item" | Find an item below a matching group at any depth. | | "/group/item" | Follow the path from the current node. | | "/" | Return the current node. |

Pass a namespace URI as the second argument to restrict matches:

const namespacedItems = root.query("item", "urn:example");
const code = namespacedItems[0]?.getAttribute("code", "urn:example");

Attribute

An Attribute exposes its name, local name, prefix, namespace uri, and string value. Calling attribute.toString() returns its value.

Development

This project uses Bun for development:

bun install
bun run security
bun test
bun run build
bun run check

License

MIT