Titanium Community Questions & Answer Archive

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

Parsing XML struct

I am seeking some help on parsing xml returned from an xmlrpc call.
An example result is a structure of name/value pairs:

<methodResponse>
<params>
<param>
<value><array>
<data>
<value><struct>
<member><name>id</name>
<value><string>8</string></value>
</member>
<member><name>name</name>
<value><string>Dr Bar</string></value>
</member>
</struct></value>
</data>
</array></value>
</param>
</params>
</methodResponse>

I have tired the following but get null so I suppose I am over simplifying.

var result = this.responseText;
Titanium.API.info('Received: '+result);
var xml = Ti.XML.parseString(result);
var structList = xml.documentElement.getElementsByTagName("struct");
Titanium.API.info(structList.length);
Titanium.API.info(structList.item(0).getElementsByTagName("name").text);
Titanium.API.info(structList.item(0).getElementsByTagName("value").text);

correction.

Titanium.API.info(structList.item(0).getElementsByTagName("name").item(0).text);

Does give me "id" but

Titanium.API.info(structList.item(0).getElementsByTagName("value").item(0).text);

Gives me zip.

Ok, correction again..

Titanium.API.info(structList.item(0).getElementsByTagName("value").item(0).getElementsByTagName("string").item(0).text);

Gives me "8".
I just need a more efficient way to do this as the structures returned by various calls are not all the same.

Ok, now I am this far I can figure it out.

New question: how do you delete a dumb question so it does not clutter up the Q&A?

Thanks.

— asked March 18th 2010 by rob stevens
1 Comment
  • I had a lot of trouble parsing XMLRpc in this environment. I found a decent parser called Mimic which took the XML and parsed into a massive array using JS. However this took up so much memory since the array was so massive that I ended up abandoning this part of the project and using a webview instead.

    — commented August 29th 2011 by dave fletcher

4 Answers

  • Here's a solution:

    var result = this.responseText;
    var xml = Ti.XML.parseString(result);
    
    var params = xml.documentElement.getElementsByTagName("member");
    var name = xml.documentElement.getElementsByTagName("name");
    var value = xml.documentElement.getElementsByTagName("string");
    var data = [];
    
    for (var i=0;i<params.item.length;i++) {
        Ti.API.log('Param '+i+': Name: '+n.item(i).text);
        Ti.API.log('Param '+i+': Value: '+v.item(i).text);
    
        // Add to array
        data.push({"name":n.item(i).text,"value":v.item(i).text});
    }
    

    Your XML doc could have a better structure, which would make it easier to parse. Here's a screenshot.

    If you use responseXML, your XML needs to be validated (ie, DTD).

    — answered March 18th 2010 by TZ Martin
    permalink
    1 Comment
    • This answer helped me a lot, but I saw an issue in the for condition: params.item.length … To fix this, just do params.length

      Thanks for the great topic guys!

      — commented September 8th 2011 by Joseandro Luiz
  • One thing you can do is to also try using : var xml = this.responseXML;

    Instead of using Ti.XML.parseString from the result.

    — answered March 18th 2010 by Kosso
    permalink
    0 Comments
  • This isnt a dumb post - it got me over my brain block with xml in this environment!

    — answered July 22nd 2011 by Steve Hovey
    permalink
    0 Comments
  • I have an XML with this structure:

    <?xml version="1.0" encoding="UTF-8"?>
    <Result>
        <User>
            <ID>65</ID>
            <Name>adm_adm</Name>
            <Email>noreply@domain.com</Email>
        </User>
        <Accounts>
            <Account ID="248" Name="wertyu" />
            <Account ID="251" Name="hgdfh" />
            <Account ID="249" Name="wertyui" />
        </Accounts>
        <Roles>
            <Rol ID="14" Name="SentDocsAsign" />
            <Rol ID="15" Name="RcvDocsAsign" />
            <Rol ID="30" Name="SenderUser" />
        </Roles>
    </Result>
    

    I parsed all the User data, but how can i parse the Accounts and the Roles nodes?

    — answered December 17th 2012 by Alex Lerma
    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.