iPhone: NSXMLParser's foundCharacters method called multiple time for single tag
i am able to parse the XML file. but i am facing strange error. My XML file format is like this
<contact>
<contactServiceTag>3B92E6A7-B599-EAE9-1CB7B5592CB8695C</contactServiceTag>
<contactDeletedBoolean>0</contactDeletedBoolean>
<contactLinkStatus>Stored</contactLinkStatus>
<contactName>P4</contactName>
−
<contactEmailAddresses>
<workEmail>updatedp4@isol.co.in</workEmail>
<personalEmail/>
<otherEmail/>
</contactEmailAddresses>
<contactLastUpdated>{ts '2010-01-22 10:05:42'}</contactLastUpdated>
<contactPhotoExists>False</contactPhotoExists>
</contact>
during the parsing, when parser parse the element contactLastUpdated , then foundCharacters method called multiple time and it return the value {ts on first run, \' on second run, 2010-01-22 10:05:42 on third run,\' on fourth run and finally }开发者_StackOverflow on last run. so i get only last value (}) when i called didEndElement method.
please suggest how can i resolve this type of error
In your implementation of the <NSXMLParserDelegate> callbacks like parser:foundCharacters:, you should be storing the found characters in instance variables, possibly concatenating a string together, so that when parser:didEndElement:namespaceURI:qualifiedName: is invoked, you have the full element value/body available to your object through its instance variable state.
You might also read up on the difference between SAX and DOM parsers. NSXMLParser is a SAX parser which is less convenient to use, but performs better than DOM parsers.
Create a string when entering an element, append to it when foundCharacters is called and then check its length/value on didEndElement?
Both Jon's and Mobs' answers are correct, that is the way to do it. In order to understand better how it works, I suggest that you take a look at Apple's Seismic XML sample project. It uses the NSXMLParser in a very clear way and also shows how to handle the situation you are in.
精彩评论