开发者

Reusing NSXMLParser

开发者 https://www.devze.com 2022-12-19 02:41 出处:网络
in a class i\'m writing i\'ll most likely have to use NSXMLParser twice to parse two different xml\'s, and i\'m wondering which approach should i use?

in a class i'm writing i'll most likely have to use NSXMLParser twice to parse two different xml's, and i'm wondering which approach should i use? - release the parser after it finished parsing u开发者_运维知识库rl and reinitialize when need to parse the second url? - use different class as delegate for parsing other url? - or something else?

thanks peter


In my own personal experience, I've commonly had to parse several different REST xml responses and for each of them I inherit a base class and create one class per request/response/parse. IMHO although this isn't clean code, I honestly find it impossible to write clean code when dealing with a SAX-style parser.

My advice would be separate calls and perhaps separate classes if you don't want a bunch of if-else's in your code. Now if the XML is very similar, it could be a different story...


I have written a class which implements the parser methods and you just have to pass it a string (your url). It returns with an array of elements. It may be of use to you.

You can download it here: http://www.kieranmcgrady.me/helper-classes-for-parsing-xml-files


In the past I've often made classes to parse each response type that I expected to see, you can reuse an NSXMLParser, but I really haven't seen a need to.

Depending on your requirements you may want to just read the responses into nested NSDictionaries, then deal with accessing the elements that you need directly from the dictionaries.

0

精彩评论

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

关注公众号