Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

xmldoc

nfarina6.8mMIT2.0.1TypeScript support: included

A lightweight XML Document class for JavaScript.

xml, sax, parser, xpath, document

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

  • name - the node name, like "tat" for <tat>. XML "namespaces" are ignored by the underlying sax-js parser, so you'll simply get "office:body" for <office:body>.
  • attr - an object dict containing attribute properties, like bookNode.attr.title for <book title="...">.
  • val - the string "value" of the node, if any, like "world" for <hello>world</hello>.
  • children - an array of XmlElement children of the node.
  • firstChild, lastChild - pretty much what it sounds like; null if no children
  • line, column, position, startTagPosition - information about the element's original position in the XML string.

Each member defaults to a sensible "empty" value like {} for attr, [] for children, and "" for val.

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.

changelog

Change Log

v2.0.0 (2024)

Major Changes:

  • Complete TypeScript rewrite with full type definitions
  • Dual package support for both CommonJS and ESM environments
  • Maintained backwards compatibility with existing code
  • Updated sax dependency to 1.2.4
  • Added support for modern module resolution via package.json exports field
  • Improved HTML compatibility with self-closing tags
  • Enhanced documentation and examples
  • Added proper TypeScript declaration files

v1.0.0 (2016-12-26)

Full Changelog

Closed issues:

  • Excellent library with a beautiful, clean API #42
  • Order of elements changed #41
  • While writing back xml document to a file it removes all comments #39
  • react native using xmldoc to parser xml #38
  • Order of val in relation to children? #37

Merged pull requests:

v0.5.1 (2016-05-12)

Full Changelog

Closed issues:

  • Release notes for 0.5 #35

Merged pull requests:

  • GLOBAL is producing deprecation warnings in node V6 #36 (jmalins)

v0.5.0 (2016-04-27)

Full Changelog

Closed issues:

  • Incorrect escaping of < > #29
  • Update tag for v0.4.0 #28
  • Error parsing coments out of XML scope #27
  • Support of xml comments #22
  • Question on usage #20

Merged pull requests:

v0.4.0 (2015-11-16)

Full Changelog

Closed issues:

  • Support DOCTYPE or ignore it please #24

Merged pull requests:

v0.3.1 (2015-05-22)

Closed issues:

  • xmldoc error #19
  • Add Error Reporting #17
  • Need Line Number #14
  • How to retrieve the value in a few level down the nodes? #13
  • childNamed returns null not undefined #12
  • New version for npm? #10
  • getValueWithPath - xml namespace not supported? #9
  • High byte characters are not coming in correctly. #8
  • Add text as child nodes #7
  • descendantWithPath() not always finds valid path #6
  • TypeError: Cannot call method 'apply' of undefined when parsing VMware vCloud Director XML #5
  • Serialization #2
  • can't create new XMLDocument… it seems to be undefined #1

Merged pull requests:

  • Escape ampersands and quotes as well #18 (protobi)
  • Added parse information to XmlElement #15 (EToreo)
  • Add escaping '<' and '>' in toString() #11 (martnst)
  • add whole document serialization via toString(whole=true, compressed=true) #4 (jankuca)

* This Change Log was automatically generated by [githubchangelog_generator](https://github.com/skywinder/Github-Changelog-Generator)_