Friday, 15 July 2011

xml parsing is done but the values are not added to the array in swift -



xml parsing is done but the values are not added to the array in swift -

<?xml version="1.0"?> <account> <username>dedehakan</username> <avatar>http://www.somewebsite.com/empty.png</avatar> <email>test@gmail.com</email> <points>450</points> <type>premium</type> <expiration>666666</expiration> <expiration-txt>17/12/2014 20:37:35</expiration-txt> <premium-left>-614239</premium-left> </account>

i want parse nsxmlparser. parsing done correctly how ever didnt values. here try:

var mydata:nsdata = resp.datausingencoding(nsutf8stringencoding, allowlossyconversion: true)! var xmlparser = nsxmlparser(data: mydata) xmlparser.delegate = self xmlparser.parse()

resp string , value of resp xml text.

func parser(parser: nsxmlparser!, didstartelement elementname: string!, namespaceuri: string!, qualifiedname qname: string!, attributes attributedict: nsdictionary!) { println(elementname) println(attributedict) }

the output is:

account nil username nil avatar nil email nil ....

the question how values in xml string?

the problem info in xml document not come attributes; comes textual content of tag itself. why these nils when print attributedict.

to prepare this, need these 4 things:

add instance variables lasttag:string? , tagcontent:string? parser delegate class set lasttag elementname in didstartelement function provide implementation of foundcharacters function appends characters tagcontent string provide implementation of didendelement function uses tag , content in whatever way need, , sets them both nil.

to create long story short, need wait until content of tag has become available until reach end tag, @ point "harvest" content instance variables.

swift

No comments:

Post a Comment