iphone上的NSxml解析器多达3个级别

问题描述

| 我在获取A-> 1,2,3,4&b-> 1,4的值时遇到问题 我必须在3张桌子上显示这些数据 表1)A,B 表2)当用户单击A->时,他可以看到表格形式的1,4 但我的情况下,我只能显示A的最后一个值,即4(而不是1,3) 如何解析所有1,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,4i处截取数据 并且B = t1,4 ** 更新 如何获得描述和评级的值?
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\"])
        {



        }

解决方法

通过观察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\"]];
}
在“ 5”法中
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;
}
谢谢,