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

xmhell

v0.1.2

Published

Read XML into a JSON structure and then convert that JSON back into XML.

Downloads

468

Readme

XMHell - If it's good enough for the web, it's good enough for config files!

Read XML into a JSON structure and then convert that JSON back into XML.

This library exports 4 functions.

  • parse(): same as JSON.parse(), takes a string and returns a javascript object.
  • write(): takes an object, a writer (anything with a function called write), and a callback. Writes the JSON object as XML and then calls the callback when complete.
  • escape(): Escapes XML entities.
  • unescape(): You get the idea.

Example:

> var XMHell = require('xmhell');
> j = XMHell.parse('<?xml?>\n<doc><x><y>abcdefg</y></x><!-- comment --></doc>');
{ doc: 
   { x: { y: 'abcdefg' },
     '#COMMENT': ' comment ' } }
> 
> XMHell.write(j, process.stdout, function() { console.log('done!'); });
<?xml version="1.0" encoding="UTF-8"?>
<doc>
  <x>
    <y>abcdefg</y>
  </x>
  <!-- comment -->
</doc>
done!
>

You can provide a function in place of a string in your JSON document and this function will be called with a writer and a callback to be called upon completion. The provided writer escapes XML entities.

> var async = function(writer, callback) { writer.write('async & stuff'); callback(); };
> XMHell.write({"a":async}, process.stdout);
<?xml version="1.0" encoding="UTF-8"?>
<a>async &amp; stuff</a>

Multiple tags by the same name are converted to numbered keys in the json hash.

> XMHell.parse('<?xml?><doc><x/><x/><x/><x/><x/><x/></doc>');
{ doc: 
   { x: '',
     'x 2': '',
     'x 3': '',
     'x 4': '',
     'x 5': '',
     'x 6': '' } }

Any trailing numbers are stripped from the key when generating the tag name.

> XMHell.write({'a 9000':1, 'a 3': 2, a:3}, process.stdout);
<?xml version="1.0" encoding="UTF-8"?>
<a>1</a>
<a>2</a>
<a>3</a>

Comments, cdata and doctypes are expressed using special keys called #COMMENT, #CDATA and #DOCTYPE.

> XMHell.parse('<?xml?><!DOCTYPE blah><!-- This is a comment --><![CDATA[yay cdata]]>');
{ '#DOCTYPE': 'blah',
  '#COMMENT': ' This is a comment ',
  '#CDATA': 'yay cdata' }

There are millions of XML parsers, why would you do such a thing?!

This parser concentrates on making it possible to represent any valid XML as something in JSON where it can be manipulated and then converted to XML. It preserves comments and prettyprints the result with 2 spaces per tab.

What if you feed it invalid XML?

No idea, how about you try and find out! If it makes you feel better, this parser is guaranteed not to generate invalid json.

Does it support attributes and namespaces?

Sure does!

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xml:lang="en-US">
</feed>

Converts to:

"feed xmlns=\"http://www.w3.org/2005/Atom\" xmlns:media=\"http://search.yahoo.com/mrss/\" xml:lang=\"en-US\"": {
}

How about custom entities?

LOL you're joking right?

This idea is bad and you should feel bad.

What did you expect? XMHeaven? Anyway this was not my idea.

Your angle bracket tax is due!

Send an invoice.