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 🙏

© 2024 – Pkg Stats / Ryan Hefner

xmldom-js

v0.0.4

Published

Lightweight schema-based XML data extraction to plain objects (JSON)

Downloads

160

Readme

Build Status

xmldom-js

Lightweight schema-based XML data extraction to plain objects (JSON)

Example

To read the data from the following XML

<?xml version='1.0' encoding='UTF-8'?>
  <rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
      <title>MRSS Title</title>

      <item>
        <title>Item 1</title>
        <media:content url="http://example.com/1.mp4" medium="video"/>
        <media:comment>Item 1 Comment</media:comment>
      </item>

      <item>
        <title>Item 2</title>
        <media:content url="http://example.com/2.mp4" medium="video"/>
        <media:comment>Item 2 Comment</media:comment>
      </item>

    </channel>
  </rss>

Data extraction schema:

import { attribute, content, string, readXML, prefixNamespace } from "xmldom-js";

var rssSchema = {
 rss: {
   channel: {
     title: content(string),
     item: [{
       title: content(string),
       "media:content": { url: attribute() },
       "media:comment": content(string)
     }]
   }
 }
};

First xml is parsed using the browser DOMParser and then read into plain JS objects using the schema:

  var rssXml = new DOMParser().parseFromString(rssString, "text/xml");

  var rssJSON = readXML(rssXml, rssSchema, prefixNamespace);

The resulting JSON:

{
  "rss": {
    "channel": {
      "title": "MRSS Title",
      "item": [
        {
          "title": "Item 1",
          "media:content": {
            "url": "http://example.com/1.mp4"
          },
          "media:comment": "Item 1 Comment"
        },
        {
          "title": "Item 2",
          "media:content": {
            "url": "http://example.com/2.mp4"
          },
          "media:comment": "Item 2 Comment"
        }
      ]
    }
  }
}

Namespace handling

A few modes for handling of the namespace are available:

  • ignoreNamespace - namespace and prefixes are ignored
  • prefixNamespace - use namespace prefixes as is ignoring namespaceURI. Should not be used if XML is provided by third-party as prefixes are not supposed to be stable, but quite safe for in-house XMLs.
  • namespaces(defaultURI, { prefix: URI }) - provide map of supported namespace with URIs.

Usage

Npm compatible packager (webpack) is required. CommonJS and ES6 modules are provided, transpiled to es5.