XML<-> JSON conversion. What do you think?

L

Lasse Reichstein Nielsen

Max said:
Lasse Reichstein Nielsen ha scritto:
What if the conversion of <e>
some
<a>textual</a>
content
</e>
was:
{"tag": "e",
"content" : [ "some", {"tag": "a", "content":
["textual"]}
"content" ]}
What is the big difference then?

For JSON, "textual" is a value of "a" and then { a: "textual" }.

That's a choice. There is nothing inherently more right about
{"a" : "textual"}
than
{"tag" : "a", "content": ["textual"]}
The latter is, however, prepared for content that is more complex than
a single text node, and allows room for attributes as well.

How about this translation:
["e","some",["a","textual"],"content"]
?
It's short and consistent, and you can add attributes as an object
literal after the tag name if necessary.
This convertion is your expansive implementation created to bypass the
JSON limitations...

Which limitations? JSON is not XML. XML is not JSON. Neither is a
superset of the other. Converting one to the other literally will
not work.
In fact, to obtain the string "some" instead of (example) e["#text"],

But using e["#text"] is meaningles, since there is more than one
text part inside the "e" element, and the order of the text nodes
wrt. the "a" element is also important.
you must use a blinded mode tag.content[1],

Yes, the first child node of the "e" element can be either text
or element, but indexing it by index will work in either way.
In JSON, if you want *ordered* composite data, you use an array.
Arrays are indexed by number.
while it is more correct to log on with the real name of the
object/tag, that is "e"!

"log on"?

The "e" is not the element. The "e" is the tag name of the element.
It is a property *of* the element, which is why {"tag":"e"} makes
perfect sense.

So, I challenge you:
Give the "more correct" translation of the following XML, and
ensure that the translation is invertible (although it need not
preserve the ordering of attributes):

<basket>
A fruit basket. It contains:
<apple apple="apple" sort="fuji">
<a href="http://www.example.com">Delicious <em>juicy</em> apple.</a>
</apple>
and
<orange sort="somesort">
Orange from <a href="http://www.espania.es">Spain</a>.
</orange>
Can you suggests to me a good XML-JSON converter?

Define "good". I.e., which properties should the converter have?
Being invertible is a must, but should the size of the result
be minmized, or is it more important that the result is

Here is *a* conversion from XML to JS, and its inverse (or rather,
from DOM nodes to JS object values, which can easily be converted to
and from XML and JSON respectively)

function XMLtoJS(node) {
if (node.nodeType == 3) { // text node
return node.nodeValue;
} else if (node.nodeType == 1) { // element node
var res = [node.tagName];
var attrs = node.attributes;
if (attrs.length > 0) {
var attrMap = {};
for(var i = 0; i < attrs.length; i++) {
attrMap[attrs.name] = attrs.value;
}
res.push(attrMap);
}
for(var c = node.firstChild; c; c = c.nextSibling) {
var cval = XMLtoJS(c);
if (typeof cval != "undefined") {
res.push(cval);
}
}
return res;
}
// else undefined
}

function JStoXML(val) {
if (val instanceof Array) {
var node = document.createElement(val[0]);
var i = 1;
var attrs = val[1];
if (attrs instanceof Object && !(attrs instanceof Array)) {
for(var name in attrs) {
node.setAttribute(name, attrs[name]);
}
i++;
}
for(;i<val.length; i++) {
node.appendChild(JStoXML(val));
}
return node;
} else if (typeof val == "string") {
return document.createTextNode(val);
}
}

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top