Git Product home page Git Product logo

xml-converter's Introduction

XML Converter

A small ServiceWorker that transparently converts on-the-fly XML responses to JSON.

xml-converter inspect all the http traffic that originates from its domain, detect XML responses and converts them into JSON before they reach the page. This is especially useful when working with a framework that does not support XML parsing.

Have you seen the GDL video?

I've made a short video that describes what is a ServiceWorker, how it works and how it can be used to create this library. If you have found the contents of the video interesting you might want to spend some time diving a bit deeper into the features of this technology.

I've wrote a few ideas about how this library could be extended. Want to give it try? Have a look at the Extending the library section!

Install

The best way to install this library is using bower:

bower install [email protected]:sandropaganotti/xml-converter.git

To enable this library this line of code needs to be added to your root ServiceWorker, if you don't have one just create an empty sw.js file in the root of your project:

importScripts('bower_components/xml-converter/xml-converter.js');

To install the root ServiceWorker (if haven't done it already) you can use this code snippet from your page:

if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/sw.js', {scope: '/'});
}

Example

Before xml-converter

<eveapi version="2">
    <currentTime>2015-02-21 09:57:30</currentTime>
    <result>
        <serverOpen>True</serverOpen>
        <onlinePlayers>24985</onlinePlayers>
    </result>
    <cachedUntil>2015-02-21 10:00:07</cachedUntil>
</eveapi>

After xml-converter

{
    "eveapi": [
        {
            "val": "",
            "attr": {
                "version": "2"
            },
            "cachedUntil": [
                {
                    "val": "2015-02-21 09:36:05",
                    "attr": {}
                }
            ],
            "result": [
                {
                    "val": "",
                    "attr": {},
                    "onlinePlayers": [
                        {
                            "val": "24637",
                            "attr": {}
                        }
                    ],
                    "serverOpen": [
                        {
                            "val": "True",
                            "attr": {}
                        }
                    ]
                }
            ],
            "currentTime": [
                {
                    "val": "2015-02-21 09:35:53",
                    "attr": {}
                }
            ]
        }
    ]
}

Extending this library

Here's a few interesting experiments that can be done using this library as a starting point, to get a better grasp of the power and the features offered by ServiceWorker:

  • Instead of converting all the responses with a Content-Type: application/xml it would be better to search instead for a specific header in the request, like X-Convert-To: JSON.
  • Extending the previous idea it would be great to have another custom header X-JSON-Filter: some.specific.key.path that instructs the ServiceWorker to return only the JSON subpath specified as the value of the header.
  • As a last step the library could support a 3rd custom header X-Cache-For: 3600. If this header is specified then the ServiceWorker can use the built-in cache to store the response, converted to JSON, for the specified amount of seconds. During this period every hit on the original request causes the ServiceWorker to return the cached JSON response.

Kudos

Nick Farina for xmldoc - https://github.com/nfarina/xmldoc

xml-converter's People

Contributors

sandropaganotti avatar

Watchers

Sandro Paganotti avatar James Cloos avatar

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.