views:

305

answers:

3

hi all, 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>[email protected]</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 } on last run. so i get only last value (}) when i called didEndElement method.

please suggest how can i resolve this type of error

A: 

Create a string when entering an element, append to it when foundCharacters is called and then check its length/value on didEndElement?

Mobs
+2  A: 

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.

Jon Hess
hi jon,foundCharacters method is called one time for all other tags but why is is not called one time for above specified tag. i just want to know this thing. however appendstring is also a alternate
Rupesh
A SAX parser scans the source text and produces these callbacks for the parser's delegate. The implementation probably scans the text with a fixed size buffer so that it can have an upper bound on memory usage. This means that you'll often get partial results. If you've ever done manual file IO with the unix read (http://www.opengroup.org/onlinepubs/000095399/functions/read.html) function, this is similar in that you shouldn't ever write your program expecting to get the values of elements in their entirety. You always need to be building them up in a buffer.
Jon Hess
A: 

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.

Dimitris