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.
Here are the rules:
Text content of elements goes in the $ property of an object.
<alice>bob</alice>
becomes
{ "alice": { "$" : "bob" } }
Nested elements become nested properties
<alice><bob>charlie</bob><david>edgar</david></alice>
becomes
{ "alice": { "bob" : { "$": "charlie" }, "david": { "$": "edgar"} } }
Multiple elements at the same level become array elements.
<alice><bob>charlie</bob><bob>david</bob></alice>
becomes
{ "alice": { "bob" : [{"$": charlie" }, {"$": "david" }] } }
Attributes go in properties whose names begin with @.
<alice charlie="david">bob</alice>
becomes
{ "alice": { "$" : "bob", "@charlie" : "david" } }
@xmlns property.The default namespace URI goes in @xmlns.$.
<alice xmlns="http://some-namespace">bob</alice>
becomes
{ "alice": { "$" : "bob", "@xmlns": { "$" : "http:\/\/some-namespace"} } }
Other namespaces go in other properties of @xmlns.
<alice xmlns="http:\/\/some-namespace" xmlns:charlie="http:\/\/some-other-namespace">bob</alice>
becomes
{ "alice": { "$" : "bob", "@xmlns": { "$" : "http:\/\/some-namespace", "charlie" : "http:\/\/some-other-namespace" } } }
Elements with namespace prefixes become object properties, too.
<alice xmlns="http://some-namespace" xmlns:charlie="http://some-other-namespace"> <bob>david</bob> <charlie:edgar>frank</charlie:edgar> </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"} } }
Plug a URL into this form to get back a Javascript expression that assigns the BadgerFish-formatted JSON object to a variable named xml.
<p>Some text <strong>is important</strong>.</p>). Perhaps those different text bits should be made available in $1, $2, etc.@xmlns in all elements where a namespace is active, not just in the elements where the namespace is declared. This may be overkill for people who don't care that much about namespaces. Perhaps allow a format that ignores @xmlns altogether?Tell me (David Sklar) what you think. The name is Brian's fault.
Jacob Smullyan wrote a related Python model, pesterfish, which he describes as: "a quick Python
module which is uses the same xml object model as the dominant xml module
in the Python world, elementtree; Some elementtree implementation (there are
several) and simplejson are required. BTW, elementtree stores namespaces in Clark notation: {http://www.w3.org/1999/xhtml}br and so does this.". pesterfish also lets you round trip XML through it without any data loss.