作为Xcode 7中iOS开发的新手。 下面我有一些问题请澄清我的疑问。
并将每行的XML数据拆分并存储在Objective C中。
<?xml version="1.0" encoding="UTF-8"?>
<NewDataSet>
<tbl>
<Es_Id>8e268283-e87e-4abc-aab9-07cb611a8e60</Es_Id>
<EstablishmentType>40640054-2221-4086-92e4-4440497ccea2</EstablishmentType>
<EstablishmentName>La Parrilla Colombian Steakhouse & Bar</EstablishmentName>
<BusinessName>La Parrilla Colombian Steakhouse & Bar</BusinessName>
<OpenTime>PT8H31M</OpenTime>
<ClosingTime>PT18H50M</ClosingTime>
<Floor>12 th floor</Floor>
</tbl>
</NewDataSet>
答案 0 :(得分:0)
当我开始学习XML解析时,它有基本的步骤。我为你实现所有。
在ViewController.h中
第1步:添加委托类
First you have to add <NSXMLParserDelegate>
第2步:创建必要的对象
NSXMLParser *parser;
NSMutableData *ReceviedData;
NSMutableString *currentStringValue;
NSMutableArray *arrayID;
现在看起来像
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<NSXMLParserDelegate>
{
NSXMLParser *parser;
NSMutableData *ReceviedData;
NSMutableString *currentStringValue;
NSMutableArray *arrayID;
}
@end
然后在ViewController.m
中第3步 - 在viewDidLoad方法中分配数组
arrayID = [[NSMutableArray alloc]init];
第4步 - 在viewDidLoad中创建连接
[self createConnection:@"http://www.google.com"]; //give your valid url.
现在viewDidLoad方法是
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
arrayID = [[NSMutableArray alloc]init];
[self createConnection:@"http://www.google.com"]; //give your valid url.
}
createConnection方法是
-(void)createConnection:(NSString *)urlString
{
NSURL *url = [NSURL URLWithString:urlString];
// Step 5 - parser delegate methods are using NSURLConnectionDelegate class or not.
BOOL success;
if (!parser)
{
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
parser.delegate = self;
parser.shouldResolveExternalEntities = YES;
success = [parser parse];
NSLog(@"Success : %c",success);
}
}
第6步 - NSXMLParserDlegate方法在
下面-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(@"Current Element Name : %@",elementName);
if ([elementName isEqualToString:@"ID"]) //according to your xml response your id is Es_Id.So you need to compare @"Es_Id"
{
NSLog(@"The Result is==%@",elementName);
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentStringValue = [[NSMutableString alloc] initWithString:string];
NSLog(@"Current String Value : %@",currentStringValue);
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"ID"]) //according to your xml response your id is Es_Id.So you need to compare @"Es_Id"
{
[arrayResult addObject:currentStringValue];
}
currentStringValue = nil;
}
从上面的代码我只检查ID。根据你的回复你需要比较其他键,如EstablishmentType,EstablishmentName,BusinessName,OpenTime .......