Freesnöw
Freesnöw

Reputation: 32143

Reading XML from Javascript, is it reasonably possible?

I'm developing a very smooth site that will display products for a company. I'm using jQuery to display the products. Problem is, I don't want to hard-code all of the products in. I want to store all of the products (product, description, price, etc.) in XML. I'm not worried about security because clients wont actually be buying anything directly from site.

With this in mind, what is the best way to load and read the XML file using Javascript/jQuery. I've read some stuff online but it all looks pretty dodgy, especially when it comes to browser support. Is this realistically possible? Should I do this another way? How can I best achieve this?

EDIT

JSON also seems like a decent alternative as well. I just need explanations on how to do it.

Thanks!

Upvotes: 0

Views: 98

Answers (3)

Ghislain Fourny
Ghislain Fourny

Reputation: 7279

I agree that it should be easier to use JSON if you are using JavaScript directly.

If you want to use XML, you might be interested in using XQuery or XSLT (which natively support XML, and almost seamlessly (X)HTML) as the scripting language.

Upvotes: 2

Tom Hubbard
Tom Hubbard

Reputation: 16129

Storing data as JSON is a cleaner bet and it works a lot more seamlessly with JavaScript.

JQuery's ajax functions have a built in JSON data type that automatically parses if for you.

I would look into JSON at json.org and then start looking at some of the JQuery docs. It's not too complicated.

Really it's just JavaScript objects in string format. You deal with them, after parsing, as normal objects.

Upvotes: 4

vzwick
vzwick

Reputation: 11044

You might want to look into XML2JSON if you can afford the conversion to take place client-side.

A server-side alternative can be found here since you might want/need to implement a same-domain proxy (also, Caching would be pretty nice, I guess?).

Upvotes: 1

Related Questions