Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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.

— asked March 19th 2010 by Shaun F
  • iphone
  • json
  • xml
1 Comment
  • What kind of error you got? some of the bugs in that code were pointed out and provided fixed version in comments section. Please read all comments

    — commented July 21st 2012 by Rajesh Rajesh

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();
    
    — answered October 11th 2011 by David Bankier
    permalink
    4 Comments
    • I'm attempting to use your js module from github, but getting one error:

      "can't find variable: exports"

      I see that commented at the top of your source code, but don't see how to fix it.

      — commented October 26th 2011 by T.J. Mahaffey
    • XMLTools.js is a CommonJS module. You need to use the require statement to import it. Have a look at the app.js example on github.

      The comment at the top /*globals exports*/ is just so JSLint doesn't complain.

      — commented October 26th 2011 by David Bankier
    • Works great! Thanks for sharing!

      — commented December 10th 2012 by Dan Boorn
    • Thanks for sharing!

      — commented January 17th 2013 by Pablo Arocha
  • 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.

    — answered March 19th 2010 by Dan Giulvezan
    permalink
    0 Comments
  • 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');

    — answered March 22nd 2010 by Shaun F
    permalink
    0 Comments
  • Did you ever get a resolution for this? How did you end up handling your XML to JSON conversion?

    Thanks!

    — answered April 3rd 2011 by Michael Stearne
    permalink
    1 Comment
    • Try this: https://github.com/dbankier/XMLTools-For-Appcelerator-Titanium The new version is bug fixed.

      — commented February 16th 2013 by Rainer Schleevoigt
  • 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);
    
    — answered July 7th 2012 by Rainer Schleevoigt
    permalink
    3 Comments
    • Can you share the file?

      — commented July 8th 2012 by David Bankier
    • <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <gpx xmlns="http://www.topografix.com/GPX/1/1" creator="OSM Route Manager" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
      <!-- All data by OpenStreetMap, licensed under cc-by-sa-2.0 (http://creativecommons.org/licenses/by-sa/2.0/). -->
          <rte>
              <name>D3 - D-Netz-Route (Niedersachsen)</name>
              <src>OpenStreetMap.org</src>
              <type>bicycle</type>
              <rtept lat="51.8863845" lon="10.6458585" />
              <rtept lat="51.886659" lon="10.6457278" />
              <rtept lat="51.8868277" lon="10.6455807" />
              <rtept lat="51.8868963" lon="10.6454045" />
              <rtept lat="51.8870046" lon="10.6447721" />
              <rtept lat="51.810533" lon="9.827263" />
              <rtept lat="51.8105427" lon="9.8273966" />
          </rte>
      </gpx>
      

      — commented July 17th 2012 by Rainer Schleevoigt
    • Noticed simular issue due to blank text nodes. Updated XMLTools.traverseTree to skip over blank child nodes of text type instead of returning the blank value. You can view the changes here:

      — commented December 10th 2012 by Dan Boorn
  • 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());
        }
    }
    
    — answered February 8th 2013 by Rainer Schleevoigt
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.