Parse xml to json in mobile
Anyone have any pointers on how this can be achieved?
I've tried to use Thomas Frank's xml2json.js converter (http://www.thomasfrank.se/xml_to_json.html) however this throws up an error when I try to include it.
6 Answers
-
I have created a commonJS module, built for Appcelerator that does this and a bit more in the same style as Thomas Frank's. It is github.
So you can do this:
var XMLTools = require("XMLTools").XMLTools; var my_json = new XMLTools(your_xml_string).toJSON();
-
Is the error you're getting a 260 error? If so, double check your path to the .js file, 260 means whatever you're trying to refer to can't be found.
Maybe you can have a PHP somewhere that handles getting the data and doing the conversion for you? Might make your app run faster too if you offload that work to something faster.
-
The error I'm getting is:
[ERROR] Script Error = * -[NSCFString appendString:]: nil argument at win1.js (line 1).
The file is in same directory the calling script.
I'm using the following line to attempt the include:Titanium.include('xml2json.js');
-
Did you ever get a resolution for this? How did you end up handling your XML to JSON conversion?
Thanks!
-
I have tested the moduel with a gpx (xml) file. The code gives me a empty string.
var xml = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, "routen/" + _e.item.url + ".gpx").read().text; var XMLTools = require("/modules/xmltools").XMLTools; var parser = new XMLTools(xml); var route = parser.toObject(); Ti.API.log(route);
-
Here is a running code which read all XML in a directory and convert to JSON:
exports.getlist = function(_qti, _callback) { var qtidir = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, _qti); if (qtidir.exists() === false) return; var dir_files = qtidir.getDirectoryListing(); for (var i = 0; i < dir_files.length; i++) { if (!dir_files[i].match(/\.xml$/i)) continue; var XMLTools = require('modules/XMLTools'); var parser = new XMLTools(Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, _qti, dir_files[i]).read().text); console.log(parser.toObject()); } }