PDA

View Full Version : Convert XML Data to JSON Data



optimalcapacity
23 Mar 2009, 6:58 AM
Hey everyone...

I am new to ExtJS and am working on a demo which utilizes ExtJS on the front-end while the backend system is based on both J2EE and Java Servlet technology. The backend system has natively handled XML output for years. While ExtJS has many of the widgets needed for this demo, I still need a few extra UI components currently available from other libraries. These other libraries currently support JSON data formats. Does ExtJS natively support the conversion of XML data to JSON data? For the demo, I will have to juggle both XMl and JSON datasets. If so, could you please direct me to the necessary code or provide me with an example? If not, any ideas and all ideas are appreciated!

I realize the best approach for this demo would be to add the server-side output of JSON. However, one of the objectives of this demo is to have legacy support for pre-existing solutions built on XML datasets. Once I can prove the effectiveness of ExtJS from the client-side, I can work on selling optimizations on the server-side to the business.

Thanks in advance!

Animal
23 Mar 2009, 7:02 AM
You will usually be reading Records into your client which are cached in Stores which provide the Model for the DataView and the GridPanel.

You will use an XmlReader to convert the received XML data.

There is full documentation in the API docs, there are examples in your examples directory, and there is lots of information in the grid FAQ.

If you have any specific questions, someone will try to answer.

evant
23 Mar 2009, 7:03 AM
There's no native support for it. In fact it would probably be a whole lot quicker to do that conversion server side.

optimalcapacity
23 Mar 2009, 7:15 AM
Thanks for the quick replies!

I agree that server-side generated JSON is preferred. However, for this demo, it really isn't an option.

After digging around the ExtJS documentation and the forums, I thought the answer would be "no" but with it's built-in support of consuming both XML and JSON formats, I thought someone had to have had the need to convert between these formats and worked out a XML-JSON bridge.

Animal
23 Mar 2009, 7:17 AM
I don't think thwe OP means XML representation to JSON representation. I think the OP means XML representation to Javascript objects. He says "JSON data" which people usually use when they mean "an object".

I don't see any use for converting one textual data interchange format into another on a client.

vishalg
23 Mar 2009, 9:15 AM
Here's a JavaScript library that can convert XML to JSON;

http://www.thomasfrank.se/xml_to_json.html

But this will not help in any of the Ext components.

optimalcapacity
23 Mar 2009, 9:24 AM
Thanks vishalg!

Animal
23 Mar 2009, 10:33 AM
I'm not clear on why you would send out XML from a server, and convert to a JSON string on the client.

Then where else would you send that string for further processing???

optimalcapacity
30 Mar 2009, 7:13 AM
The web application solution that I have been working with has been around for over 8 years. Up to this point, the native delivery format of data to the client has been XML. While I could easily add support for JSON data and move forward with this approach, the server-side implementation of JSON would require server-side architectural changes for existing customers solutions. The focus of my demo is to offer a client-side alternative to the existing product offering. This will only be successful if I can minimize the number of architectual impacts. In other words, a few extra lines of client-side code isn't as painful as both client and server required revisions.

The issue that I am running into is that some of the JS libraries required for this demo will consume data in JSON data format only. So to minimize the number of requests to the server (and since ExtJS can consume XML or JSON data), I was curious to see if anyone has created a bridge between these data stores.

Thanks!

Animal
30 Mar 2009, 7:46 AM
XmlReader?

optimalcapacity
30 Mar 2009, 9:23 AM
Let me try this from a different perspective.

Is there a native way with ExtJS to make an xhr request to a server to return XML data and output the XML as a string? If so, could an example please be provided?

Thanks!

deanna
30 Mar 2009, 9:35 AM
Let me try this from a different perspective.

Is there a native way with ExtJS to make an xhr request to a server to return XML data and output the XML as a string? If so, could an example please be provided?

Thanks!

Any browser, any js library, retrieves whatever the server sends in response to its request. If the server sends xml that is what the client sees. It is then up to the client what to do with it. If you want it to output that as a string, you don't do anything with it, it is a string when it is received. If you want to use it as data, then you use the xmlreader.

optimalcapacity
30 Mar 2009, 10:02 AM
Agreed.

Here's the "perfect world" scenario I was researching:

1) Make XHR to return data in XML format (Native format returned by web application)
2) Use XmlReader to load data into ExtJS UIs
3) Also allow data interoperability across to other JS libraries and their widgets which natively support JSON data

From the previous posts in this thread, both 1 and 2 are inherent with ExtJS. However, the ability to convert data from XML Data to JSON Data is not inherently possible with ExtJS.

What I am wondering is there a native ExtJS property for XmlReader or another data store that I am missing which will output the XML data to a string format (like ect.value, ObjObject.text, Object.innerHTML, or the like). If so, then I could use Thomas Frank's xml2json script (http://www.thomasfrank.se/xml_to_json.html) to convert the XML to JSON on the client. This would eliminate the need for a second XHR of the same data which already exists client-side.

Thanks.

optimalcapacity
30 Mar 2009, 10:03 AM
typo...like ect.value, ObjObject.text, Object.innerHTML, or the like...should have read...like Object.value, Object.text, Object.innerHTML, or the like

deanna
30 Mar 2009, 10:07 AM
Once you use the xmlreader, the data is in javascript object format, which the JSON.encode function can then output as a json string.

Animal
30 Mar 2009, 10:10 AM
But surely, if we are talking about Javascript, once the XML data is read into memory structures, we don't want to go back to a string format again.

Can't these other mysterious libraries pull in data? Like Store.loadData (http://localhost:8080/docs/?class=Ext.data.Store&member=loadData)?

You surely don't want to actually go back to a string representation?

optimalcapacity
30 Mar 2009, 10:33 AM
Animal, I think we are on the same page and I completely agree with all your points.

Thanks everyone for your assistance!