Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

What's the easiest way to parse an XML response in the companion code?

I'm working on a simple app which accesses a CALDAV server.  Responses are in XML.  It appears as though Fitbit standardized on JSON a little while back and deprecated XML support.  Fine and understandable.  Given that, what's the easiest way to parse a relatively simple XML response?  Write my own recursive XML parser? 

Best Answer
0 Votes
7 REPLIES 7

I would like to know an answer to this as well.

 

Through the Simulator I am able to use DomParser in order to parse the data:

var parser = new DOMParser();
var xmlDoc = parser.parseFromString(data,"text/xml");

However, once pushed to the Versa I receive: Can't find variable: DOMParser.

 

This discontinuity from the Simulator to the device has me at a loss.

Best Answer
0 Votes
I just needed a simple parser, so I just wrote my own. I'd be glad to share it with you, if you want. It really wasn't hard to write a simple recursive routine, but it also isn't a complete parser by any means.


/raj
(sent from iPhone)
Best Answer
0 Votes

Thank you Raj, I actually did the same after I wrote here.

Best Answer
0 Votes

Hi Rajid,

I have the same problem right now. Would you mind sharing your parser with me?

 

Kind regards,

Schete

Best Answer
0 Votes

Here's my simple, basic, recursive, parser.  It called my parseCalendar() routine in order to actually parse the calendar entry, but the rest is simply a basic parse of the XML.  I hope it's useful as a starting point.  (If you're trying to deal with actual calendar information on a Fitbit watch, they now have an API specifically to do that, which is must easier.

 

function xmlparse(xml,index) {

    xml = xml.trim();

 

    while (xml.length > 0) {

        if (xml.indexOf("<!") == 0) {

            let e = xml.indexOf(">");

            //      console.log(`Calendar data is: ${xml.substring(2,e)}`);

            parseCalendar(xml.substring(2,e),index);

            return xml.substring(e+1);

        }

        if (xml[0] != '<') {

            let e=xml.indexOf("<");

            //    console.log(`Body='${xml.substring(0,e)}`);

            return xml.substring(e);

        }

        

        // Save the initial component name

        let comp = xml.search(/[ >]/);

        //  console.log(`comp=${comp}`);

        let component = xml.substring(1,comp);

        //  console.log(`component is '${component}'`);

        // Find the closing brace

        let compEnd = xml.search(/>/);

        

        // Now parse the rest

        let remain = xmlparse(xml.substring(compEnd+1),index);

 

        if (!remain || remain.length == 0) {

            return "";

        }

 

        remain.trim();

        if (remain.indexOf("</" + component) == 0) {

            // This is my component end

            let compEnd = remain.search(/>/) + 1;

            //    console.log(`compEnd is: '${remain.substring(compEnd)}'`);

            return remain.substring(compEnd);

        }

        

        // Parse next thing at this level

        xml = remain.trim();

        //  console.log("** Next thing at this level");

    }

}

Best Answer

Thank you for sharing!

Best Answer
0 Votes

What’s good 

Best Answer
0 Votes