Monday, April 17th, 2006
BadgerFish: Translating XML to JSON
Brian McCallister says that conversation started, innocently enough, discussing serialization strategies for web services... and out came BadgerFish:
BadgerFish is a convention for translating an XML document into a JSON object. Once you've got your XML document represented as a JSON object, it's easy to manipulate from within Javascript. If you're familiar with PHP's SimpleXML extension, think of BadgerFish as aiming for a similar goal: making it simpler to do common manipulations of XML documents with a predictable structure.
The Rules
- Element names become object properties
- Text content of elements goes in the $ property of an object.
- Nested elements become nested properties
- Multiple elements at the same level become array elements.
- Attributes go in properties whose names begin with @.
- Active namespaces for an element go in the element's @xmlns property.
- The default namespace URI goes in @xmlns.$.
- Other namespaces go in other properties of @xmlns.
- Elements with namespace prefixes become object properties, too.
So,
-
-
<alice xmlns="http://some-namespace" xmlns:charlie="http://some-other-namespace"> <bob>david</bob> <charlie :edgar>frank</charlie> </alice>
-
Becomes,
-
-
{ "alice" : { "bob" : { "$" : "david" , "@xmlns" : {"charlie" : "http:\/\/some-other-namespace" , "$" : "http:\/\/some-namespace"} } , "charlie:edgar" : { "$" : "frank" , "@xmlns" : {"charlie":"http:\/\/some-other-namespace", "$" : "http:\/\/some-namespace"} }, "@xmlns" : { "charlie" : "http:\/\/some-other-namespace", "$" : "http:\/\/some-namespace"} } }
-












<heresy tr=”http://www.w3.org/TR/REC-xml-names/”>
Does it have an option to NOT emit the namespace stuff? I can’t imagine a javascript client application really using that in a real world way
</heresy>
David Sklar is to blame! That said, it has been ported to ruby by Paul Battley as well =)
Would it be too much to ask that the magick xmlns attribute not pollute the “normal” attribute namespace?
I could have an element named xmlns, couldn’t I?
How about normal attribs get prefixed with “@”, while namespaces get prefixed with “:”?
Interesting Finds
It looks like this is only for PHP and Ruby users. If it was a Javascript library it would be server-language neutral, right?
Jeremy — you actually can’t have an attribute named “xmlns” (or, that is, if you did, it has to be what’s setting the namespace). And if you had a child element named ‘xmlns’, it wouldn’t collide with the attribute — the attribute is “@xmlns” while the child element is “xmlns”.
Jon — the convention on translation from XML to JSON is language neutral. You are right, however, that so far libraries have only been written (that I am aware of) for PHP and Ruby. I have been noodling around with a Javascript version as well, but I need to fix a few IE-specific bugs in it before it’s ready for release.
[...] BadgerFish: Translating XML to JSON (tags: ajax json php javascript) [...]
My english is very bad, but I believe that the communication can be established…
I developed a JSON - XML translator in Javascript and, to do that with BadgerFish, i found some difficulties.
There is no orientation to work with comments or CDATA sections…
I didn’t find anything about it, because of that i used arbitrarily the characters “!” for comments and ” #” for CDATA sections, besides implementing the suggestion to the text fragments in $1, $2, … properties.
The script was not very tested, but it looks good. Works well.
Test: http://www.pousadabocaina.com.br/webmaster/json/jsontest.html
[...] The conversion follows the convention set by BadgerFish. I didn’t handle namespaces though. [...]