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 🙏

© 2025 – Pkg Stats / Ryan Hefner

xmldoc

v2.0.3

Published

A lightweight XML Document class for JavaScript.

Downloads

7,597,429

Readme

Build Status Coverage Status

Introduction

xmldoc lets you parse XML documents with ease. It's a lightweight XML document class with a single dependency on the excellent sax parser.

For more on why I wrote this class, see the blog post.

As of version 2.0, xmldoc fully supports TypeScript and can be imported in both CommonJS and ESM environments.

Release Notes

See CHANGELOG.md for details.

Installation

npm install xmldoc
# or
yarn add xmldoc

Or just download the repository and include it in your node_modules directly. Or just download the single JS file!

Usage

CommonJS (Node.js)

const { XmlDocument } = require("xmldoc");

const document = new XmlDocument("<some>xml</some>");

// do things

ESM / TypeScript

// ESM environments
import { XmlDocument } from "xmldoc";

const document = new XmlDocument("<some>xml</some>");

React Native

If you're using React Native, you may need to install buffer and stream separately:

npm install buffer stream xmldoc

Classes

The primary exported class is XmlDocument, which you'll use to consume your XML text. XmlDocument contains a hierarchy of XmlElement instances representing the XML structure.

Both XmlElement and XmlDocument contain the same members and methods you can call to traverse the document or a subtree.

Members

| Member name | Default empty value | Description | | :----------------------------------------------- | :------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | name | | Node name, like "tat" for <tat>. XML "namespaces" are ignored by the underlying sax-js parser: so <office:body> becomes "office:body". | | attr | {} | Object dict containing attribute properties. Like bookNode.attr.title for <book title="...">. | | val | "" | String "value" of the node, if any. Like "world" for <hello>world</hello>. | | children | [] | Array of XmlElement children of the node. | | firstChild | | What it sounds like. null if no children. | | lastChild | | What it sounds like. null if no children. | | line, column, position, startTagPosition | | Information about the element's original position in the XML string. |

Methods

All methods with child in the name operate only on direct children; they do not do a deep/recursive search.

It's important to note that xmldoc is designed for when you know exactly what you want from your XML file. For instance, it's great for parsing API responses with known structures, but it's not great at teasing things out of HTML documents from the web.

If you need to do lots of searching through your XML document, I highly recommend trying a different library like node-elementtree.

eachChild(func)

Similar to underscore's each method, it will call func(child, index, array) for each child of the given node.

childNamed(name)

Pass it the name of a child node and it will search for and return the first one found, or undefined.

childrenNamed(name)

Like childNamed but returns all matching children in an array, or [].

childWithAttribute(name,value)

Searches for the first child with the given attribute value. You can omit value to just find the first node with the given attribute defined at all.

descendantWithPath(path)

Searches for a specific "path" using dot notation. Example:

<book>
  <author>
    <name isProper="true">George R. R. Martin</name>
    ...
  </author>
  ...
</book>

If you just want the <name> node and you have the XmlElement for the <book> node, you can say:

var nameNode = bookNode.descendantWithPath("author.name"); // return <name> node

valueWithPath(path)

Just like descendantWithPath, but goes deeper and extracts the val of the node. Example:

var authorName = bookNode.valueWithPath("author.name"); // return "George R. R. Martin"

You can also use the @ character to request the value of a particular attribute instead:

var authorIsProper = bookNode.valueWithPath("author.name@isProper"); // return "true"

This is not XPath! It's just a thing I made up, OK?

toString([options])

This is just an override of the standard JavaScript method, it will give you a string representation of your XML document or element. Note that this is for debugging only! It is not guaranteed to always output valid XML.

The default implementation of toString(), that is, the one you get when you just console.log("Doc: " + myDoc) will pretty-print the XML with linebreaks and indents. You can pass a couple options to control the output:

xml.toString({ compressed: true }); // strips indents and linebreaks
xml.toString({ trimmed: true }); // trims long strings for easier debugging
xml.toString({ preserveWhitespace: true }); // prevents whitespace from being removed from around element values
xml.toString({ html: true }); // uses HTML self-closing tag rules for elements without children

Putting it all together:

var xml = "<author><name>looooooong value</name></author>";
console.log(
  "My document: \n" + new XmlDocument(xml).toString({ trimmed: true }),
);

Prints:

My Document:
<hello>
  loooooooo…
</hello>

Feedback

Feel free to file issues or hit me up on X.