Iphone 如何从文件中解析XML
可能重复:Iphone 如何从文件中解析XML,iphone,xml-parsing,Iphone,Xml Parsing,可能重复: 我的Xcode项目中有一个XML文件,我想解析这个XML文件并在UITableView中显示数据。因为我是xml解析新手,所以对我来说做同样的事情变得越来越困难。如何解析下面的xml 我的xml文件是: <application> <app_data> <wayHome> <data> <title>159 Darby St Bus Stop&
我的Xcode项目中有一个XML文件,我想解析这个XML文件并在UITableView中显示数据。因为我是xml解析新手,所以对我来说做同样的事情变得越来越困难。如何解析下面的xml 我的xml文件是:
<application>
<app_data>
<wayHome>
<data>
<title>159 Darby St Bus Stop</title>
<category>BUS</category>
<lat>-32.9320366</lat>
<long>151.7712731</long>
<image>159 Darby St Bus Stop.png</image>
</data>
<data>
<title>Beaumont St and Denison St Taxi Rank</title>
<category>TRAIN</category>
<lat>-32.9320366</lat>
<long>151.7712731</long>
<image>Beaumont St and Denison St Taxi Rank.png</image>
</data>
<data>
<title>Beaumont St Bus Shop next to ANZ</title>
<category>BUS</category>
<lat>-32.9227304</lat>
<long>151.7472369</long>
<image>Beaumont St Bus Shop next to ANZ.png</image>
</data>
</wayHome>
</app_data>
</application>
达比街159号公共汽车站
公共汽车
-32.9320366
151.7712731
159达比街公共汽车站.png
博蒙特街和丹尼森街出租车站
训练
-32.9320366
151.7712731
博蒙特街和丹尼森街出租车站.png
澳新银行旁边的博蒙特街巴士店
公共汽车
-32.9227304
151.7472369
ANZ.png旁边的博蒙特街巴士店
使用NSXMLParserDelegate方法解析文件
In yourInterface.h file include NSXMLParserDelegate
@implementation yourInterface.m file: use below code
NSURL* xmlFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"yourXMLfile"
ofType:@"xml"]];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlFile];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
NSLog(@"No Errors");
else
NSLog(@"Error Error Error!!!");
使用委托方法来解析文件。
我假设您需要解析“数据”标记。
在这里,您可以检查ElementName是否为“数据”。对于“Data”的innerTag也类似。在这里,您可以维护数据元素的数组或字典
因此,您的工作是在委托方法中执行:
- (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;
问题是你想解析什么??您要选择哪个标签?请至少把问题框好…谢谢你的建议。我想解析标题、类别、纬度、长度、图片。