NSX解析器在iPhone上最多3级

时间:2011-04-20 06:46:12

标签: iphone

我有一个问题是获得A-> 1,2,3,4&的价值b-> 1,2,3,4

我必须在3张桌子上显示这些数据

表1)A,B 表2)当用户点击A - >他可以看到1,2,3,4的桌面形式 但我的情况我只能显示A的最后一个值,即4(不是1,2,3)

如何解析所有1,2,3,4

<?xml version="1.0" encoding="UTF-8"?>
<hall>
<movie Name="A">
<stall Name="1">
<description>qwe.</description>
<rating>5</rating>
</stall>
<stall Name="2">
<description>wer.</description>
<rating>5</rating>
</stall>
<stall Name="3">
<description>wer.</description>
<rating>5</rating>
</stall>
<stall Name="4i">
<description>wer.</description>
<rating>5</rating>
</stall>
</movie >
<movie Name="B">
<stall Name="t1">
<description>wer.</description>
<rating>5</rating>
</stall>
<stall Name="2">
<description>wer.</description>
<rating>5</rating>
</stall>
<stall Name="3">
<description>wer.</description>
<rating>5</rating>
</stall>
<stall Name="4">
<description>wer.</description>
<rating>5</rating>
</stall>
</movie>
</hall>

更新

我的问题是我不知道如何分隔电影名称A和B数据

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {

    if([elementName isEqualToString:@"hall"])
    {
        movieList = [[NSMutableDictionary alloc]init];
            appDelegate.books = [[NSMutableArray alloc]init];
        appDelegate.HospN = [[NSMutableArray alloc]init];
    }
    else if([elementName isEqualToString:@"movie"])
    {
        currentMovie = [[NSString alloc] initWithString:[attributeDict objectForKey:@"Name"]] ;
        [appDelegate.books addObject:[attributeDict objectForKey:@"Name"]];


        //[aBook.movie addObject:[attributeDict objectForKey:@"Name"]];
        NSLog(@"Processing Value: %@", currentMovie);
        //aBook.testament=currentMovie;

        aBook.stall = [attributeDict  objectForKey:@"name"];

        NSLog(@"Movie A name : %@",[movieList objectForKey:@"A"]);


    }
    else if([elementName isEqualToString:@"stall"])
    {
        [appDelegate.HospN addObject:[attributeDict objectForKey:@"Name"]];
        NSLog(@"Processing stall Value: %@", currentElementValue);

        NSLog(@"appDelegate.HospN: %@", appDelegate.HospN);

    }

}

- (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:@"hall"])
    {
        //NSLog(@"Movie List : %@",movieList);
    }
    else if([elementName isEqualToString:@"movie"])
    {
        [movieList setObject:appDelegate.books forKey:currentMovie];


        NSLog(@"Movie name : %@",appDelegate.books );



        NSLog(@"Movie A name : %@",[movieList objectForKey:@"A"]);
        [currentMovie release];
        //[stallNames release];
        currentMovie = nil;
        //stallNames = nil;
    }
    else if([elementName isEqualToString:@"stall"])
    {
        //you can populate other attributes here if stall is not just string but a modal class.
    }

    else 

        [aBook setValue:currentElementValue forKey:elementName];

    [currentElementValue release];
    currentElementValue = nil;
}

我的数组我的appDelegate.HospN:值是(         1,         2,         3,         4I,         T1,         2,         3,         4     ) **但我想在A = 1,2,3,4i

处砍掉数据

和B = t1,2,3,4 **

Updtate

如何获取描述和评级的值?

else if([elementName isEqualToString:@"description"])
        { 
            NSLog(@"desp List : \n%@\n",currentElementValue);// i can see all value but how to chop data based on A and B


            [movieList setObject:currentLink forKey:@"description"];
            [appDelegate.despArray addObject:[movieList copy]];


            NSLog(@"adding story: %@",currentLink);

        }



    else if([elementName isEqualToString:@"rating"])
        {



        }

1 个答案:

答案 0 :(得分:0)

通过观察xml,你会发现这样的关系......

一个大厅可以有一部或多部电影,每部电影都可以有一个或多个电影。

所以你能做的就是。

在xml解析类中有一个实例字典,比如movieList,一个实例数组,例如stallNames和一个movieName,NSString。

movieList:将电影名称与各自的档位映射。(NSMutableDictionary) stallNames:将包含与特定电影相关的档位。(NSMutablArray) currentMovie:将具有当前电影名称,因为它将用作movieList字典中的键。(NSString)

现在在startElement方法......

if([elementName isEqualToString:@"hall"])
{
    movieList = [[NSMutableDictionary alloc]init];
}
else if([elementName isEqualToString:@"movie"])
{
    currentMovie = [[NSString alloc] initWithString:[attributeDict objectForKey:@"Name"]] ;
    stallNames = [[NSMutableArray alloc]init];
}
else if([elementName isEqualToString:@"stall"])
{
    [stallNames addObject:[attributeDict objectForKey:@"Name"]];
}
didEndElement方法

中的

if([elementName isEqualToString:@"hall"])
{
   NSLog(@"Movie List : %@",movieList);
}
else if([elementName isEqualToString:@"movie"])
{
   [movieList setObject:stallNames forKey:currentMovie];
   [currentMovie release];
   [stallNames release];
   currentMovie = nil;
   stallNames = nil;
}
else if([elementName isEqualToString:@"stall"])
{
    //you can populate other attributes here if stall is not just string but a modal class.
}

注意:就像上面发布的代码一样,你有一个Book类,它似乎代表了Movie,所以你可以做的是,有另一个属性,它将是数组类型摊位名称。这个Book对象将在稍后的数组中添加(当你遇到elementName“movie”时,在didEndElement中)。将此数组作为实例变量,并在startElement中遇到“hall”标记时对其进行初始化。

更新ANS (根据新规范)

//declare following instance variables. I am leaving aBook variable because I am not sure of its use.
NSMutableDictionary *movieList;
NSMutableArray *stallArray;
NSMutableString *currentMovieKey;


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {

    if([elementName isEqualToString:@"hall"])
    {
        movieList = [[NSMutableDictionary alloc]init]; // it will contain list of all the movies in the xml.
    }
    else if([elementName isEqualToString:@"movie"])
    {
        currentMovieKey =  [[NSMutableString alloc] initWithString:[attributeDict objectForKey:@"Name"]] ; // will be used for setting key for stallArray.
        stallArray= [[NSMutableArray alloc] init];  // it will contain stall names.

    }
    else if([elementName isEqualToString:@"stall"])
    {
        [stallArray addObject:[attributeDict objectForKey:@"Name"]];        
    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

    if(!currentElementValue) 
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    else
        [currentElementValue appendString:string];

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if([elementName isEqualToString:@"hall"])
    {
        //parsing finished...end of root tag. movieList will be the final dictionary that will have your data
        NSLog(@"Movie List : \n%@\n",movieList);
    }
    else if([elementName isEqualToString:@"movie"])
    {
        [movieList setObject:stallArray forKey:currentMovieKey];

        [currentMovieKey release];
        currentMovie = nil;
        [stallArray release];
        stallArray = nil;
    }
    else if([elementName isEqualToString:@"stall"])
    {
        //you can populate other attributes here if stall is not just string but a modal class.
    }
    //I was not sure about your else part... 

    [currentElementValue release];
    currentElementValue = nil;
}

谢谢,