Parser XML with NSXMLParser

后端 未结 2 1935
攒了一身酷
攒了一身酷 2021-01-14 09:40

I got a question with xml parsing. Normally,the XML file style like this:


      abc 
      19 
         


        
相关标签:
2条回答
  • 2021-01-14 10:03

    Reading the section under "Attribute Handling" in Apple Event-Driven XML Programming and you will have your answer.

    http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/XMLParsing/Articles/HandlingElements.html#//apple_ref/doc/uid/20002265-1001969

    0 讨论(0)
  • 2021-01-14 10:09

    I assume you want all the "elements" of a forecast-period to be added to the same dictionary you put index and start-time-local in?

    Your original basic if-statement posted in the question is fine but the problem is that with the NSXMLParser, each xml tag ("forecast-period", "element", "text", etc) causes the didStartElement method to fire.

    Since there are multiple "forecast-period" tags and multiple "element" tags, your code will need to keep track of what the current parent tag/object is. After the "forecast-period" tag is processed, didStartElement will fire again for the following "element" tag. In that call, the method parameters will have no indication that this "element" is a child of "forecast-period" with "index" 1. Your code has to keep track of that.

    Also, since the "element" tags have actual values (eg. "Sunny" for type=precis), you'll have to implement foundCharacters and didEndElement.

    In didEndElement, based on what the current target object is, you have to put the data in the proper place. If it's the end of "forecast-period", do nothing (it has no value). If it's the end of an "element" tag, you have to take the value captured by foundCharacters and add it to the last dictionary in weatherLoad. This also means you need to change to using an NSMutableDictionary instead of an NSDictionary.

    The link that @itsaboutcode gave you has a good example of all the above (it's above the "Handling Attributes" section.

    0 讨论(0)
提交回复
热议问题