开发者

Parse xml file with same tag multiple times iphone sdk

In my application, I have a tag multiple times. I'm using xml parser. I'm taking a corres开发者_Python百科ponding element with similar name as the one in xml file in my class. So in case of:

 <photo>abc</photo>
 <photo>def</photo>

What I get in photo element of my class is the second element i.e def, as the first one gets overwritten as there's only one photo element in my class. My question is am I wrong in taking similar elements in class as in case of xml? Is there any better method or a better parser? Or I'm on right path and have to do this manually by setting some flags etc?

Thanx in advance.


I assume you try to parse the contents of an XML to match to properties on an object using NSXMLParser.

If it is valid to encounter multiple photo tags in your XML then either you need a strategy to know which one will map to a property, or convert your property to an NSArray and add the results as they come.

If it is not a valid case, you could check if you set the property earlier and raise an error, or just override it (as you do) and call it "undefined behavior".


There are many parsers there that can handle your issue (e.g. XPathQuery, TouchXML etc.).

I don't think that there is a need to reinvent the wheel - use one of the existing parsers.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜