2

I have a server Implementation of ECMA Script including the ability to use E4X. Because this is pretty elegant for people don't know JavaScript and JSON Notation and we want to make an API which is most easy to learn i want to use this for my API.

I'm currently evaluating if i can use this in my environment. One Showstopping feature that i must use is to convert those XML Objects of E4X into JSON compatible JavaScript Objects or Strings on the fly. I can't use XSLT here because i have to stay inside JavaScript.

So the question is, is there an easy way to convert E4X XML Elements into JSON? Or do i have to write some code to convert it myself?

Chris
  • 7,675
  • 8
  • 51
  • 101

1 Answers1

-1

You can use XSLT to convert your XML to JSON.

For instance using: http://code.google.com/p/xml2json-xslt/

However you can end up with a very XMLish and unnecessarily complex JSON. That will make your code more difficult to write and maintain.

An API is generally meant to be stable in time, so may be some dedicated XSLT for each calls may be a better option than a generic one.

Mic
  • 24,812
  • 9
  • 57
  • 70
  • Thanks for yur answer but i fear, that i can't use xslt because i have to stay in the server environment (which is limitted to JavaScript or rather ECMA Script). Or can i actually do xslt transformations inside JavaScript? So i'm not talking about XML files here. I'm talking about E4X XML Elements inside JavaScript. I think here lies the misunderstanding. – Chris Mar 17 '11 at 14:38
  • May be you can add the XSLT stylesheet reference `` in the XML response, and the browser should convert it – Mic Mar 17 '11 at 14:43
  • Mmmm... that can't work together with JSONP... Are you sure you can't make a XSLT transformation on your server? This a fairly basic feature of all XML parsers around. – Mic Mar 17 '11 at 14:45
  • One more thing... in some browsers you can do this transformation in Javascript. But I never did that on server. – Mic Mar 17 '11 at 14:46
  • Again, thanks for all your answers but there is not a single server feature i can use except of the JavaScript engine. I'm not inside a Browser at all! This project is not a webpage or something. I just wanted to know if there is an easy way to convert objects of class E4X XML to plain old JavaScript objects. – Chris Mar 18 '11 at 08:15