|
(the previous post, titled 'Arrr', was posted by me I must have accidentally pressed the wrong button, or the pirate in me wanted to have his say)
To me, the most important difference between working with plain javascript object structures (such as JSON) and working with XML using E4X is that the latter is an example of array programming.
Things like "html.body.p.a" aren't possible to express in most programming languages. The important thing is that in E4X that expression means something completely different than the same expression in the context of a regular JSON structure.
In E4X "html.body" returns a trampoline object (XMLList), which asks all the elements in it for their "p" children, returning yet another trampoline, which asks all it's elements for their "a" children. if the expression is "html.body.p.a.text()" the text method is called on all "a" nodes and a list of all the results is returned. When working with JSON, "html.body" returns one object, or a list of objects. If it's a list we have to write a loop to do something with the children. Just getting the text content of all "a" nodes would require three nested loops (depending on the actual data structure). Of course the code behing E4X uses loops, but the point is that we don't have to.
|