简体   繁体   English

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

[英]NSxml parser upto 3 levels on iphone

i have a issue getting the value of A->1,2,3,4 & b->1,2,3,4 我在获取A-> 1,2,3,4&b-> 1,2,3,4的值时遇到问题

i have to show these data on 3 tables 我必须在3张桌子上显示这些数据

table 1) A , B table 2)when user click on A -> he can see 1,2,3,4 in tablular form but i my case i can only show the last value of A ie 4 ( not 1,2,3) 表1)A,B表2)当用户单击A->时,他可以以表格形式看到1,2,3,4,但在我的情况下,我只能显示A的最后一个值,即4(不是1,2, 3)

how to parse all 1,2,3,4 如何解析所有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>
#

UPDATES 更新

#

my problem is i dont know how to separate Movie name A and B data 我的问题是我不知道如何分隔电影名称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; } 

my array my appDelegate.HospN: value is ( 1, 2, 3, 4i, t1, 2, 3, 4 ) **but i want to chop data at A= 1,2,3,4i 我的数组我的appDelegate.HospN:值是(1,2,3,4i,t1,2,3,4)**但是我想在A = 1,2,3,4i处砍数据

and B= t1,2,3,4** 并且B = t1,2,3,4 **

Updtate 更新

how to get the value of description and rating ?? 如何获得描述和评级的值?

 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"]) { } 

By observing the xml you will find the relationship like this... 通过观察xml,您会发现这样的关系...

A hall can have one or more movie and Each movies can have one ore more stall. 一个大厅可以有一个或多个电影,每个电影可以有一个或多个摊位。

so what you can do is. 所以你可以做的是

have a instance dictionary, say movieList, and an instance array say,stallNames, and one movieName,NSString in your xml parsing class. 在xml解析类中有一个实例字典,例如movieList,和一个实例数组,stallNames,以及一个movieName,NSString。

movieList : will have mapping of movie name with their respective stalls.(NSMutableDictionary) stallNames : will contain stalls related to particular movie.(NSMutablArray) currentMovie: will have current movie name as it would be used as key in movieList Dictionary.(NSString) movieList:将具有电影名称与各自档位的映射。(NSMutableDictionary)stallNames:将包含与特定电影相关的档位。(NSMutablArray)currentMovie:具有当前电影名称,因为它将用作movieList字典中的键。(NSString)

Now in startElement method ... 现在在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"]];
}

in didEndElement method 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.
}

Note : as in above code posted by you , you have a Book class, and it seems it represents Movie, so what you can do is, have another property which is array type which will hold stall names. 注意 :就像您在上面发布的代码中一样,您有一个Book类,并且它似乎表示Movie,因此,您可以做的是,拥有另一个属性,该属性是将持有摊位名称的数组类型。 and this Book object will be added later on an array(in didEndElement when you will encounter elementName "movie"). 并且此Book对象将稍后添加到数组上(当您遇到elementName“ movie”时,在didEndElement中)。 make this array as instance variable and initialize it when you encounter "hall" tag in startElement. 使此数组作为实例变量,并在startElement中遇到“ hall”标记时对其进行初始化。

UPDATE ANS (AS PER NEW CODE) 更新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;
}

Thanks, 谢谢,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM