AcidJs.XmlToJson() JavaScript class is a simple XML to JSON converter and objectifier. It can be used with custom or public XML, REST APIs such as Last.FM, RSS and ATOM feeds, sitemaps, etc by converting the XML server responses to native JSON objects in the browser which are then instantly accessible by its simple to use JavaScript methods.
Requirements
- Modern browser with native JSON support. To enable in older browsers, use Douglas Crockford’s JSON2 or one of the various solutions on the Internet.
- Web Server.
- PHP.
Usage and API
Copy the entire AcidJs.XmlToJson/ directory from the distribution file in the root of your project, then register the run-time script before the closing </body> tag of the page:
<script src="AcidJs.XmlToJson/Scripts/AcidJs.XmlToJson.js"></script>
(function() {
window.xmltojson = new AcidJs.XmlToJson();
})();
If you wish to keep the AcidJs.XmlToJson/ directory on another location, you can use the optional rootFolder property in the object initializer, and then make sure you also change the path in the SRC attribute of the script tag you used to register the XmlToJson runtime:
<script src="Scripts/AcidJs.XmlToJson/Scripts/AcidJs.XmlToJson.js"></script>
(function() {
window.xmltojson = new AcidJs.XmlToJson({
rootFolder: "Scripts/" // do not forget the trailing slash
});
})();
Once you initialize XmlToJson you will have access to the following methods:
getRemoteData(config)
Request XML, convert it to JSON on the server, then set and optionally return the JSON object. In the config you can defin the following properties: name, url, params (optional) and success callback (optional):
window.xmltojson.getRemoteData({
name: "slayer", // unique name that will be used to request the converted XML to JSON object locally
url: "http://ws.audioscrobbler.com/2.0/", // remote / RESTFul service URL
params: [{
method: "album.getinfo"
},{
api_key: "XXXXXX" // your Last.FM API key
},{
artist: "Slayer"
},{
album: "Seasons in the Abyss"
}
],
success: function(data) {
// the success call back returns the newly created data object that contains the converted JSON object and the config of the getRemoteData method
console.log("data object: ", data); // return the entire data object
console.log("JSON object: ", data.json); // return the converted XML to JSON object only
console.log("data object name: ", data.name); // return the name of the data object
console.log("request url: ", data.url); // return the request url
console.log("request parameters as objects: ", data.params); // return the remote request parameters as objects
console.log("removte request with the params as get data: " + data.remoteUrl); // return the removte request with the params as get data
}
});
getLocalData(name)
Returns the local instance of an existing JSON data object by it’s name (set in the name property of the config of the getRemoteData method)
window.xmltojson.getLocalData("slayer");
deleteData(data)
Deletes an array of stored data object(s) by specified name and makes them unavailable.
window.xmltojson.deleteData(["slayer", "rss", "feedburner"]);
Examples
Get Artist and Album Data from Last.FM (http://last.fm):
window.xmltojson.getRemoteData({
name: "slayer", // unique name that will be used to request the converted XML to JSON object locally
url: "http://ws.audioscrobbler.com/2.0/", // remote / RESTFul service URL
params: [{
method: "album.getinfo"
},{
api_key: "XXXXXX" // your Last.FM API key
},{
artist: "Slayer"
},{
album: "Reign in Blood"
}
],
success: function(data) {
console.log(data.json);
}
});
Get RSS feed from Feedburner (http://feedburner.com)
window.xmltojson.getRemoteData({
name: "feedburner",
url: "http://feeds.feedburner.com/acidmartin",
success: function(data) {
console.log(data.json);
}
});
Parse Sitemap File
window.xmltojson.getRemoteData({
name: "feedburner",
url: "http://acidjs.wemakesites.net/sitemap.xml.php",
success: function(data) {
console.log(data.json);
}
});
Using XmlToJson
- Download.
- Check the demo page.
- Include on your page. Refer to the demo page included in the file you have downloaded.
- Have fun and don’t forget to check Acid.JS Web.UI components or follow @wemakesitesnet on Twitter
Related Posts
- JavaScript Mapper Class for RESTFul Methods
- HTML Validation Bookmarklet
- JavaScript Template Engine
- URI to JSON Serializer
- IMDB Fetcher – Experimental IMDB JavaScript and PHP API
- Quick and Dirty Localization With JavaScript
- JavaScript Instance Name Finder
- Cross-browser HTML5 Placeholder Enabler
- Cross-browser HTML5 Details Element Enabler
- JavaScript Wrapper for the HTML5 WebWorkers API
- JavaScript Inheritance Without Constructors
- JavaScript Class Augmenter
- JavaScript API for TinyURL
[...] Visit publisher site: XML to JSON Converter [...]
[...] XML to JSON JavaScript Objectifier Class – Simple XML to JSON objectifier. It can be used with custom or public XML, REST APIs such as Last.FM, RSS and ATOM feeds, sitemaps, etc by converting the XML server responses to native JSON objects in the browser which are then instantly accessible by its simple to use JavaScript methods. [...]
[...] XML to JSON JavaScript Objectifier [...]
[...] XML to JSON JavaScript Objectifier [...]
[...] XML to JSON JavaScript Objectifier [...]
[...] XML to JSON JavaScript Objectifier [...]
[...] XML to JSON JavaScript Objectifier [...]
[...] XML to JSON JavaScript Objectifier [...]
[...] XML to JSON JavaScript Objectifier [...]
[...] XML to JSON JavaScript Objectifier [...]
[...] XML to JSON JavaScript Objectifier Applying Special CSS Rules for IE6-IE8 and IE9 Without Affecting Other Browsers [...]
[...] XML to JSON JavaScript Objectifier [...]
[...] XML to JSON JavaScript Objectifier [...]
[...] XML to JSON JavaScript Objectifier [...]
[...] XML to JSON JavaScript Objectifier Class [...]