Search code examples
xmlappcelerator

Parse and extract data from XML using Appcelerator


Titanium SDK version: 1.6.1
iPhone SDK version: 4.2

I am having a hard time extracting the data from this XML output in Appcelerator. I would like to get the token and the username. How is this done?

<rsp stat="ok">
<auth>
<token>73257626300602415-3324a12587e2e9b3602</token>
<perms>delete</perms>
<user nsid="599323339@N047" username="theuser200" fullname="" />
</auth>
</rsp>

Thankful for all input.


Solution

  • Presuming your xml came from am xhr request then.

    xhr.onload = function() {
      var doc = this.responseXML.documentElement; // Give me a xml document.
      var token = doc.getElementsByTagName("token").item(0).text; // Get the token element, then the first item (could be lots of them) then the text of the first.
      var username = doc.getElementsByTagName("user").item(0).getAttribute("username"); // Ditto as above, but this time get the attribute name "username"
    }