繁体   English   中英

如何将XML正确解析为iPhone

[英]How to parse XML to iPhone correctly

我有一个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;
}

您能给我们错误信息吗? 要在iPhone上解析,您可以使用外部库。 以下是一些与它们相关的测试:http: //www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project NSXMLParser实际上是简单的事件解析器。

暂无
暂无

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

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