如何在iOS中解析这种类型的XML数据?

时间:2014-02-20 12:41:28

标签: ios objective-c xml-parsing

如何解析enclosure_details属性?这是我的XML输出。

<enclosure>
                    <enclosure_details url="http://cdn..com/media/gallery/p18gmcacuj1o4n1572b1m1qnel1c6.jpg" length="12345" type="image/jpeg" title=<![CDATA[]]> description=<![CDATA[&lt;p&gt;Mergers and acquisitions have gained steam in early 2014. Here is a rundown of the whole-bank deals, shedding of noncore businesses and swapping of securities so far this year.&lt;/P&gt;&lt;P align=&quot;right&quot;&gt;&lt;em&gt;Image: Thinkstock&lt;/em&gt;&lt;/p&gt;]]> />
                    <enclosure_details url="http://cdn..com/media/gallery/p18gmcacuj1527me8acrk2r7td7.jpg" length="12345" type="image/jpeg" title=<![CDATA[Carolina Domino Effect?]]> description=<![CDATA[&lt;p&gt;VantageSouth in Raleigh, N.C., and Yadkin Financial in Elkin, N.C., announced plans in late January for &lt;a href=&quot;http://www..com/issues/179_18/vantagesouth-yadkin-deal-could-spur-meaningful-ma-in-north-carolina-1065227-1.html&quot;&gt;a $300 million merger&lt;/a&gt;. VantageSouth CEO Scott Custer will lead the bank. The MOE is expected to force other North Carolina banks to decide whether to become buyers or sellers.&lt;/P&gt;&lt;P align=&quot;right&quot;&gt;&lt;em&gt;Bloomberg News&lt;/em&gt;&lt;/p&gt;]]> />
                    <enclosure_details url="http://cdn..com/media/gallery/p18gmcacuj332qvhj7c1qbucn98.jpg" length="12345" type="image/jpeg" title=<![CDATA[Cost-Cutting Focus]]> description=<![CDATA[&lt;p&gt;ConnectOne in Englewood, N.J., is merging with Center Bancorp in Union, N.J., in &lt;a href=&quot;http://www..com/video/connectone-ceo-explains-banks-merger-with-center-of-nj1065072-1.html&quot;&gt;another MOE-like deal&lt;/a&gt; that would create a $3 billion-asset bank and is meant to produce $7 million in annual cost savings. Frank Sorrentino of ConnectOne will become the CEO.&lt;/p&gt;]]> />

1 个答案:

答案 0 :(得分:1)

尝试使用适用于iOS的NSXMLParser:https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSXMLParser_Class/Reference/Reference.html

你在这里有一个教程:http://iphonebyradix.blogspot.com/2011/08/parsing-xml-using-nsxmlparser.html

此委托方法可以获取标记

中的字符串
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
}

此方法为您提供了包含enclosure_details

内部网址的属性
- (void)parser:(NSXMLParser *)parser foundAttributeDeclarationWithName:(NSString *)attributeName forElement:(NSString *)elementName type:(NSString *)type defaultValue:(NSString *)defaultValue;