GithubHelp home page GithubHelp logo

commenthol / xml-vs-js Goto Github PK

View Code? Open in Web Editor NEW
2.0 4.0 0.0 44 KB

Convert XML/ HTML to Javascript and vice versa

License: The Unlicense

JavaScript 100.00%
converter html-to-json javascript xml-to-json xml-to-html xml-to-js

xml-vs-js's Introduction

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

xml-vs-js's People

Contributors

commenthol avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

xml-vs-js's Issues

TypeError: cb is not a function

nodejs "test.js"
/home/phil/node_modules/xml-vs-js/src/toJs.js:116
    cb(null, out)
    ^
TypeError: cb is not a function
    at Object.onend (/home/phil/node_modules/xml-vs-js/src/toJs.js:116:5)
    at Parser.onend (/home/phil/node_modules/htmlparser2/lib/Parser.js:324:32)
    at Tokenizer._finish (/home/phil/node_modules/htmlparser2/lib/Tokenizer.js:835:12)
    at Tokenizer.end (/home/phil/node_modules/htmlparser2/lib/Tokenizer.js:826:25)
    at Parser.end (/home/phil/node_modules/htmlparser2/lib/Parser.js:352:18)
    at Object.toJs (/home/phil/node_modules/xml-vs-js/src/toJs.js:137:10)
    at Object.<anonymous> (/home/phil/js/test.js:9:19)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)

from:

function say(text)
{process.stderr.write(text+"\n");
}

const parser = require('xml-vs-js');

const xml = ""

var json = parser.toJs(xml);

say(JSON.stringify(json));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.