这是我第一次使用xmls,所以如果我的问题质量低,请不要苛刻。我在app上工作,我使用服务器中的xml文件检索所有数据。
在此处发布问题之前,我阅读了一些关于xml文件解析的Tutorials。但是当我开始在我的应用程序中实现它时,当我尝试解析第二个Xml文件时,我完全感到困惑,等等。
现在我想用一些截图来解释我的应用流程,这样每个人都可以轻松地找到我想要的东西。
现在当我点击第一个视图中的任何一行时,它会转到第二个视图并在第二个视图中显示所有相关数据,当我点击第二个视图中的任何项目时,它会转到第三个视图并显示有关该特定项目的详细信息。
在解释应用流程后,现在我展示了我到目前为止所尝试的代码。在教程之后,我只需将类(XMLParser,Book)拖到我的应用程序中进行测试。
代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *url = [[NSURL alloc] initWithString:@"Firstviewxmlurl"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
XMLParser *parser = [[XMLParser alloc] initXMLParser];
[xmlParser setDelegate:parser];
BOOL success = [xmlParser parse];
if(success)
NSLog(@"No Errors");
else
NSLog(@"Error Error Error!!!");
}
现在我的XMLParser.m代码是
- (XMLParser *) initXMLParser {
[super init];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"Collections"]) {
//Initialize the array.
appDelegate.books = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"Collection"]) {
//Initialize the book.
aBook = [[Book alloc] init];
//Extract the attribute here.
aBook.bookID = [[attributeDict objectForKey:@"id"] integerValue];
NSLog(@"Reading id value :%i", aBook.bookID);
}
NSLog(@"Processing Element: %@", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(@"Processing Value: %@", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"Collections"])
return;
if([elementName isEqualToString:@"Collection"]) {
[appDelegate.books addObject:aBook];
[aBook release];
aBook = nil;
}
else
[aBook setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
最终我的第一个视图类代码是
#pragma mark Table Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//return [tblViewData count];
return [appDelegate.books count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.selectionStyle=UITableViewCellSelectionStyleGray;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}
//cell.textLabel.text=[[tblViewData objectAtIndex:indexPath.row]
// stringByReplacingOccurrencesOfString:@"*" withString:@""];
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
cell.textLabel.text = aBook.title;
if([deviceType isEqualToString:@"ipad"])
{
[cell.textLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
}
//cell.textLabel.font=[UIFont fontWithName:@"Walkway_SemiBold" size:16];
return cell;
}
在实现上述所有代码后,我得到this形式的结果现在,此时我混淆了如何通过我的第二个视图xml文件url然后第三个视图url.my xml文件在此格式
第一个xml文件
<?xml version="1.0" encoding="UTF-8"?>
<Collections> <Collection id="5"><title>Allegroo</title> </Collection>
第二个xml文件
<?xml version="1.0" encoding="UTF-8"?>
<Collection id="5"><colletciontitle>Allegroo</colletciontitle> <weaveimage>"imageurl" </weaveimage><Rug id="48"><Rugthumb>"firstimageurl"</Rugthumb><RugSku>AL-637</RugSku></Rug><Rug id="51"><Rugthumb>"Secondimageurl"</Rugthumb><RugSku>AL-641</RugSku></Rug>
第三个xml文件
<?xml version="1.0" encoding="UTF-8"?>
<Rug id="47"><Rugmainimage>"imageurl"</Rugmainimage> <Rugtitle>AL-636 Aged Indigo / Vintage Beige</Rugtitle> <Rugdiscription>Hand Knotted
Distressed Wool Pile / Cotton Foundation </Rugdiscription><Availablesizes><Size>10x14</Size><Size>12x15</Size><Size>2x3</Size><Size>3x5</Size><Size>4x6</Size><Size>6x9</Size><Size>8x10</Size><Size>9x12</Size><Runner>2'6"x10'</Runner><Runner>2'6"x12'</Runner><Runner>2'6"x8'</Runner></Availablesizes></Rug>
任何与我的问题和建议相关的链接或示例代码形式的帮助都会受到高度赞赏。谢谢
答案 0 :(得分:2)
嗯,所以定义长话短说,你想要按顺序解析多个网址?
所以答案就是这样,
使用委托方法切换到下一个xml解析
- (void)parserDidEndDocument:(NSXMLParser *)parser {
// Change URL accordingly
NSURL *url = [[NSURL alloc] initWithString:@"Firstviewxmlurl"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
XMLParser *parser = [[XMLParser alloc] initXMLParser];
[xmlParser setDelegate:parser];
BOOL success = [xmlParser parse];
}
答案 1 :(得分:1)
我添加了一些代码让你明白。尽量根据你的情况实施。下面的代码只是引用,因为我没有做出你需要首先理解的所有更改,然后相应地实现
//int i = 0; // in AppDelegate
代码:
//Or ViewDidLoad Method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int = 1; //For First View
NSURL *url = [[NSURL alloc] initWithString:@"Firstviewxmlurl"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
XMLParser *parser = [[XMLParser alloc] initXMLParser];
[xmlParser setDelegate:parser];
BOOL success = [xmlParser parse];
if(success)
NSLog(@"No Errors");
else
NSLog(@"Error Error Error!!!");
}
XMLParser.m
- (XMLParser *) initXMLParser
{
[super init];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"Collections"])
{
//Initialize the array.
if (int == 1)
appDelegate.books = [[NSMutableArray alloc] init];
else if (int == 2)
appDelegate.secondArray = [[NSMutableArray alloc] init];
else if (int == 3)
appDelegate.thirdArray = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"Collection"])
{
if (i == 1)
{
//Initialize the book.
aBook = [[Book alloc] init];
//Extract the attribute here.
aBook.bookID = [[attributeDict objectForKey:@"id"] integerValue];
NSLog(@"Reading id value :%i", aBook.bookID);
}
else if (i == 2)
{
//Second Xml
}
else if (i == 3)
{
//Third Xml
}
}
NSLog(@"Processing Element: %@", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
//Check condition
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(@"Processing Value: %@", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
//Check COndition
if([elementName isEqualToString:@"Collections"])
return;
if([elementName isEqualToString:@"Collection"]) {
[appDelegate.books addObject:aBook];
[aBook release];
aBook = nil;
}
else
[aBook setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}