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

xml-vs-js

v2.1.0

Published

A xml to js to xml transformer

Downloads

130

Readme

xml-vs-js

Convert XML/ HTML to Javascript and vice versa

NPM version Build Status

Features:

  • Uses htmlparser2 to convert from xml to js.
  • Processes html as well as xml - default is xmlMode=true.
  • Respects order of elements with _elems array.
  • Intented for manipulating/ parsing feeds or xml files where a full DOM is not required.
  • Usage of plain JSON object for easy storage.
  • Handles different xml namespaces.

Why another xml to js converter?

xml-js uses sax for xml conversion which requires valid xml as input. For manipulation of xml files where input and output is xml, the compact format does not respect the correct order of elements. Non-compact mode on the other hand is cumbersome for access of nodes. I wanted to have same JSON format for in- and output.

For syntax of the Js Object please refer to the test fixtures in ./test/fixtures

Table of Contents

Conversion from XML to Js

toJs(xml, opts, (err, obj) => {})
  • see https://github.com/fb55/htmlparser2/wiki/Parser-options
  • {String} xml
  • {Object} [opts] - htmlparser2 options
  • {Object} [opts.xmlMode=true] - xmlMode is set by default; Set to false for html
  • {Object} [opts.decodeEntities=false] - decode entities
  • {Object} [opts.recognizeSelfClosing=true] - recognize self closing tags in html
  • {Object} [opts.recognizeCDATA=true] - recognize CDATA tags in html
  • {Boolean} [opts.elems] - set to false if output shall not contain _elems fields; order of xml elements is not guarateed any longer.
  • {Boolean} [opts.attrs] - set to false if output shall not contain any attributes _attrs fields;
  • {Boolean} [opts.ns] - set to false if output shall not contain any namespace _ns fields;
  • {Function} cb - callback(err, obj)

Example toJs

const {toJs} = require('xml-vs-js')

const xml = `
<?xml version="1.0" encoding=utf-8 ?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Example Feed</title>
  <entry>
    <title>Entry</title>
    <link href="http://example.org/2003/12/13/atom03" />
    <link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"/>
  </entry>
  <entry>
    <title>Update</title>
  </entry>
</feed>
`

toJs(xml, (err, obj) => {
  console.log(obj)
  /*
  {
    _elems: ['_PROCESSING', 'feed'],
    _PROCESSING: {
      _text: '?xml version="1.0" encoding=utf-8 ?'
    },
    feed: {
      _attrs: {
        xmlns: 'http://www.w3.org/2005/Atom'
      },
      _elems: ['title', 'entry', 'entry'],
      title: {
        _elems: ['_text'],
        _text: 'Example Feed'
      },
      entry: [{
        _elems: ['title', 'link', 'link'],
        title: {
          _elems: ['_text'],
          _text: 'Entry'
        },
        link: [{
          _attrs: {
            href: 'http://example.org/2003/12/13/atom03'
          }
        }, {
          _attrs: {
            rel: 'alternate',
            type: 'text/html',
            href: 'http://example.org/2003/12/13/atom03.html'
          }
        }]
      }, {
        _elems: ['title'],
        title: {
          _elems: ['_text'],
          _text: 'Update'
        }
      }]
    }
  }
*/    
})

Promises API

const {toJs} = require('xml-vs-js/promises')

const obj = await toJs(xml, opts)

Conversion from Js to XML

toXml(obj, opts, (err, xml) => {})
  • {Object} obj - the object to convert to xml
  • {Object} [opts] - options
  • {Boolean} [opts.xmlMode=true] - xmlMode is set by default; Set to false for html
  • {Boolean} [opts.encodeEntities=false] - encode entities

Example toXml

Note: _elems are optional. In case the field is missing the order of elements returned with Object.keys() is used.

const {toXml} = require('xml-vs-js')

const obj = {
  root: {
    _COMMENT: ' example wo order of elements ',
    section: {
      _attrs: { class: 'blue' },
      span: ['one', 'four'],
      _text: 'three',
      strong: 'two'
    }
  }
}
toXml(obj, (err, xml) => {
  console.log(xml)
  // <root>
  //  <!-- example wo order of elements -->
  //  <section class="blue">
  //  <span>one</span><span>four</span>three<strong>two</strong>
  //  </section>
  // </root>
})

Promises API

const {toXml} = require('xml-vs-js/promises')

const xml = await toXml(obj, opts)

Example toXml with order of elems

const {toXml} = require('xml-vs-js')

const obj = {
  _PROCESSING: {
    _text: '?xml version="1.0" encoding="utf-8"?'
  },
  root: {
    section: {
      _elems: ['span', 'strong', '_text', 'span'],
      span: ['one', 'four'],
      _text: 'three',
      strong: 'two'
    }
  }
}
toXml(obj, (err, xml) => {
  console.log(xml)
  // <?xml version="1.0" encoding="utf-8"?>
  // <root>
  //  <section>
  //  <span>one</span><strong>two</strong>three<span>four</span>
  //  </section>
  // </root>
})

Simplify object

toObj(obj, opts)
  • {Object} obj - the object to simplify
  • {Object} [opts] - options
  • {Boolean} [opts.elems] - if false remove all _elems props
  • {Boolean} [opts.attrs] - if false remove all _attrs props
  • {Boolean} [opts.ns] - if false remove all _ns props

To further simplify the object structure from toJS use the toObj method:

const {toJs, toObj} = require('../promises.js')

const xml = `
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Example Feed</title>
  <entry>
    <title>Entry</title>
    <link href="http://example.org/2003/12/13/atom03" />
    <link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"/>
  </entry>
  <entry>
    <title>Update</title>
  </entry>
</feed>
`

const obj = await toJs(xml)
const simple = toObj(obj, {elems: false, attrs: true})
console.log(simple)
/*
{
  feed: {
    _attrs: { xmlns: 'http://www.w3.org/2005/Atom' },
    title: 'Example Feed',
    entry: [
      {
        title: 'Entry',
        link: [
          { _attrs: { href: 'http://example.org/2003/12/13/atom03' } },
          {
            _attrs: {
              rel: 'alternate',
              type: 'text/html',
              href: 'http://example.org/2003/12/13/atom03.html'
            }
          }
        ]
      },
      { title: 'Update' }
    ]
  }
}
*/

License

Unlicense https://unlicense.org

References