2.6.06 # Converting Between XML and JSON @ XML.com

/img/xml_json.gif xml.com has published my article Converting Between XML and JSON.

The conversion scripts are here.

Update:

There are some bug fixes and updates available to xml2json.js and json2xml.js. Read more in the download section.

15 comments

I was looking for the JSON 2 XML and vice-versa script mentioned in the article?
 

the conversion scripts can be found now in the download section.
 

A really nice article!

Funny enough I wrote xml2json converter too (about a year ago). It mainly differs from yours in that it does not use the browsers DOM for conversion (which might make it more suitable when your running JavaScript in other environments that a browser).

Check it out here:
http://www.thomasfrank.se/xml_to_json.html
 

Thank you for the link. It seems to be an interesting solution. Especially as you are using a selfwritten XML parser based on regular expressions. Thus you are indeed able to work on pure XML text.

In contrast I prefer to rely on available XML to DOM parsers (which you can also use standalone).

A short test of your converter shows:

* it doesn't differentiate between textual element content and attributes. So a conversion back to original XML isn't possible .. if necessary of course.
* it doesn't seem to work properly with elements with both attributes *and* textual content, as in <e name="value">text</e>.

Thanks for your comment. If your internal XML parser is more reliable, I would be highly interested in that cool piece of software.
 

Thanks for the constructive critisism Stefan!

I agree with you about the two problems you have found. But I think I should be able to overcome them :-) Give me a week or something and I'll post a new version.

Just need to freshen up my understanding of some of the reg exps I've used since I wrote it a year ago. ;-)
 

Hi Stefan

I recently wrote my own XML <-> JSON converter for .NET and your article was quite helpful as a starting point. Thanks.

You can find an example here:
http://www.newtonsoft.com/blog/archive/2006/07/11/656.aspx
 

This is an awesome script - thank you very much for writing it!

One caveat: it doesn't understand CDATA section. Just found this out the hard way. The script goes into an endless loop in the 'removeWhite' method since a CDATA section is neither nodeType 1 or nodeType 3, and n can never progress unless the current node is one of these. Easy to fix that part, but actually gettting it to return the CDATA sections is another story. Any suggestions?

Thanks!
Dan
 

Hi Stefan,

When playing around with your json2xml code I found it helpful to make a small modification. For example, instead of this:

if (v instanceof Array) {
for (var i in v)
xml += ind + toXml(v[i], name, ind+"\t") + "\n";

I use this:
if (v instanceof Array) {
for (var i in v)
if(!(i in Array.prototype))
xml += ind + toXml(v[i], name, ind+"\t") + "\n";

So that prototyped methods are not picked up which was generating invalid xml.

cheers
 

@dan,

thanks for your bug report about CDATA sections. I have resolved it and also implemented bidirectional CDATA support.

@russel,

you are right about that issue. I have resolved it simply using an indexed for-loop:


for (var i=0, n=v.length; i<n; i++)
xml += ind + toXml(v[i], name, ind+"\t") + "\n";


You can read more about the updates in the download section.
 

Hi, Prof. Stefan,

I am very interested in vector 2D. However, I found that a^.b = -ax by + ay bx , which I derived by your definition for a^, is different to that you give. I am not sure which one is right because my a^ . b = - your a^ . b.
 

Yes, you are absolutely right. I changed a^ * b consistently to a * b^, as it has to be. Thanks.
 

Thank you for your issue. I need to more flexible solution and developed code based on event-event handler paradigm (like SAX). JSON tree walker and JSON path evaluator that parameters by different event handlers are core of this library: http://sourceforge.net/projects/jsontools/.

I would be very pleased to your opinion.
 

Hi,

Thanks for your xml2json script. I just wanted to let you know that I've made two additions.

1. On line 15: I've added an X.escape() around the nodeValue of the attribute.
2. In the escape() function there were a few characters missing that are supposed to be escaped. Also see: http://www.ietf.org/rfc/rfc4627.txt (2.5).

Thanks again,

Arjen
 

I added a comment about XSugar to your XML.com article, but I'll add it here as well:

I've recently come across XSugar (http://www.brics.dk/xsugar/). It seems ideally suited for XML-JSON translation, using a single rule set which can be used in both directions.

From the web site:
"An XSugar specification is built around a context-free grammar that unifies the two syntaxes of a language. Given such a specification, the XSugar tool can translate from alternative syntax to XML and vice versa. Moreover, the tool statically checks that the transformations are reversible (i.e. bidirectional) and that all XML documents generated from the alternative syntax are valid according to a given XML schema."

I have used it for some reasonably serious work, including conversion between the normal algebraic form of an equation and MathML, and it works as advertised.
 

Hi Stefan,

I have restructured your code and made sure it validates in JSLint. Thanks for the great script, hope you like the modified version I'm using! (Link provided on my name above)

Best regards,
Michael Schøler
Denmark
 
Kommentar veröffentlichen