Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > XML<-> JSON conversion. What do you think?

Thread Tools

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

Lasse Reichstein Nielsen
Posts: n/a
Max <(E-Mail Removed)> writes:

> 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"}
{"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:
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):

A fruit basket. It contains:
<apple apple="apple" sort="fuji">
<a href="">Delicious <em>juicy</em> apple.</a>
<orange sort="somesort">
Orange from <a href="">Spain</a>.

> 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[i].name] = attrs[i].value;
for(var c = node.firstChild; c; c = c.nextSibling) {
var cval = XMLtoJS(c);
if (typeof cval != "undefined") {
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]);
for(;i<val.length; i++) {
return node;
} else if (typeof val == "string") {
return document.createTextNode(val);

Lasse Reichstein Nielsen
DHTML Death Colors: <URL:>
'Faith without judgement merely degrades the spirit divine.'
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Lib to generate XML/JSON[P] output from a DTD/XSD/JSON Schema/etc Acácio Centeno Python 1 02-15-2013 07:34 AM
I am facing an issue while decoding json string using json.loads sajuptpm Python 2 12-28-2012 07:16 AM
[ANN] Security Fix json-1.1.7 for json_pure and json gems Florian Frank Ruby 0 06-30-2009 05:18 PM
"JSON for ASP" at Tuğrul Topuz ASP General 1 06-27-2008 11:37 PM
ATTN Programmers: WE pay YOU $1.00 if you let us find you a job. jdg Doris Cox Perl 0 12-02-2003 11:07 AM