Как правильно разобрать XML на iPhone - PullRequest
1 голос
/ 22 февраля 2012

У меня есть XML-файл, но я не знаю, что я сделал неправильно, так как приложение упало, я не знаю, как его решить. Может ли кто-нибудь помочь дать мне некоторые комментарии.

<list>
<section name="Texts">
        <item>
    <postID>26</postID>
    <title>A sample quiz</title>
    <url>http://yahoo.com</url>
    <score>20</score>
    </item>
    <item>
    <postID>230</postID>
    <title>A sample quiz</title>
    <url>http://www.yahoo.com</url>
    <score>20</score>
    </item>         
</section>
<section name="Images">
    <item>
    <postID>27</postID>
    <title>A sample image quiz</title>
    <url>www.google.com</url>
        <score>10</score>
    </item> 
<item>
    <postID>27</postID>
    <title>A sample image quiz</title>
    <url>www.google.com</url>
        <score>10</score>
    </item>     
</section>
</list>

Код, который я использовал:

-(allChallengeParser *)initXMLParser
{
    self = [super init];

    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.allChallengeArray = [[NSMutableArray alloc]init]; 
    xmlChallengeRoot =[[NSMutableArray alloc]init]; 
    return self;
}

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

    //Implement method called NSXMLParser when it hits the start of an element: 
    if([elementName isEqualToString:@"list"]) 
    {   
        NSLog(@"list root element found – create a new instance of challenge OBj...");
        allChallengeObj = [[allChallenge alloc] init];
        //We do not have any attributes in the user elements, but if
        // if have can extract them here: 
        // user.att = [[attributeDict objectForKey:@"<att name>"] ...];
    }
    else if([elementName isEqualToString:@"section"])
    {   
        allChallengeObj = [[allChallenge alloc] init];        


        allChallengeObj.section = [attributeDict valueForKey:@"name"];
        NSLog(@"allChallengeObj.section %@", allChallengeObj.section);
    }    
}

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

    if(!currentElementValue)
        // init the ad hoc string with the value  
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    else
        // append value to the ad hoc string 
        [currentElementValue appendString:string];    
    NSLog(@"Processing Value for: %@", currentElementValue);    
}

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

    if ([elementName isEqualToString:@"list"]) {
        return;
    }    
    if ([elementName isEqualToString:@"section"]) {
        // We are done with user entry – add the parsed user 
        // object to our user array
        [xmlChallengeRoot addObject:allChallengeObj];
        // release user object
        allChallengeObj = nil;
    }
    else
    {
        // The parser hit one of the element values. 
        // This syntax is possible because User object 
        // property names match the XML user element names
        NSLog(@"elementName %@",elementName);
        NSLog(@"current Element Value %@",currentElementValue);
        [allChallengeObj setValue:currentElementValue forKey:elementName];
    }
    currentElementValue = nil;
}

1 Ответ

0 голосов
/ 22 февраля 2012

Не могли бы вы дать нам сообщение об ошибке? Для разбора на iPhone вы можете использовать внешние библиотеки. Вот некоторые тесты, связанные с ними http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project NSXMLParser - действительно простой анализатор событий.

...