开发者

How NSXMLParser is working

I am a newbie in xcode development. I am trying to implement a web service call for fetching the values from a server and shows it on a table view.I got some sample code from net and some how i am able to make it work. but still i 开发者_如何学编程don't know how the parser iterate through the xml nodes. Can anybody help me to find out the working of xml parser in xcode and how it is going through all the nodes without a for loop?


Actually there are five delegate methods

1.didstartdocument

2.didstartelement

3.found characters

4.didendelement

5.didenddocument

In this first method is for starting of the document. second method is for if any starting elements found then, If starting element is having data it goes to the found characters. After that goes to the didendelement.After that again it goes to didstartelement.it is going on repeatedly.if document ends then it goes to didenddocument.


NSXMLParser take data from you and parse the data, on its parsing it send appropriate delegates to give data back to you. It works some thing like it do the basic XML parsing then send you the data in the form of delegate --- so you don't need loop from your end to parse XML.


This is very simple, for its proper flow you can just put breakpoint at it's method then check value of all the parameters you will understand. Still for more details do consider these methods for putting breakpoints

[self parseXMLFileAtURL:path];

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

Your element is detected in didStartElement where you allocate memory to object in which you want to store value, and in didEndElement you have the values which you store in your permanent array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜