Yoan Blanc’s weblog

Another lost swiss guy

August 2009

XML with Padding

Yoan BlancMon 17 August 2009, ,

Last week we had a small arguing on #openweb about whether JSON is better than XML. The only reason to me JSON is really beating XML is called JSONP (JSON with Padding). It’s a way to get data from a different domain using JavaScript. That data is formatted in JSON (which stands for JavaScript Object Notation). I’ll show how to send data formatted in XML which the huge drawback to only work on Gecko (because it relies on a technology that only it supports: E4X).

But as we are forced to use Gecko we should simple go for the Cross-domain XmlHTTPRequest, it’s simpler and safer.

JSONP works by appending a script to the page that will call a function we gave it with the data we expect:

callback({hello:"World!"});

for example. It’s totally unsafe and you have to really trust that third party. XMLP relies on the fact that E4X makes XML first citizen, like RegExp, Array or any other native type.

var a = <root>I’m the root</root>;

So, XMLP might look like:

callback(<hello>World!</hello>);

On Gecko (reading Firefox and bros), E4X can be enabled by adding eax=1 to the type:

<script type="text/javascript;e4x=1" …></script>

E4X is quite interesting to play with. Unfortunately Gecko and Flash are the only two platforms where you can use it. By Gecko I mean SpiderMonkey and Rhino (the two JavaScript engines Mozilla branded).

Grab this very basic example application for Python.

Voici ma première invention inutile; même d’autres y ont pensé avant moi pas d’illusion sur ce fait là. L’idée est de transposer le fonctionnement de JSONP à XML. JSONP est le moyen d’importer des données venant d’ailleurs et de les récupérer et de les traiter chez soi, comme on l’entend. Les célèbres MashUps, aka combinaisons de différentes sources de données ou outils en un nouvel élément se servent souvent de JSONP (via Y! Pipes ou YQL par exemple).

Dans un long débat concernant le bien fondé d’un JSON vs XML; JSONP me semblait le seul élément qui offrait un avantage intéressant à JSON. En tant compte de tous les problèmes de sécurité. JSONP ressemble à ceci:

maFonction({message: "Salut !"});

Ceci est fournit par un autre serveur est est inclu via une conventionnelle balise scripte. XMLP repose sur le fait que grâce à E4X (ECMAScript for XML), XML devient un élément natif de JavaScript.

var doc = <racine>Salut !</racine>;

Donc, il est également possible d’envoyer ça à sa fonction dite de callback :

maFonction(<racine>Salut !</racine>);

Si c’est assez brillant, le problème réside dans le fait que ça n’est uniquement supporté par Spidermonkey (et Rhino); le moteur JavaScript de Firefox ainsi que les produits Flash.

Petite astuce pour activer E4X, spécifier via le type du scripte.

<script type="text/javascript;e4x=1" …></script>

Firefox supportant dans les versions récentes les requêtes sur d’autres domaines, il est bien plus viable de passer par XmlHTTPRequest.

Bref, c’était un petit amusement, que je vous invite à essayer vous même: xmlp.py.

About

meYoan Blanc is a web developer that lives in Norway (Markveien 24, 0554 Oslo) works for Opera and comes from La Chaux-de-Fonds. This web site is for this weblog, a playground for random stuffs and can help keeping me connected with some friends out there.

Get my vCard or contact me by phone (skype:yoan.blanc) or email ().

Misc

RSS, list.blogug.ch

This site reflects only my opinion and is not affiliated with anyone else.

copyright 2006-2009 — doSimple.ch