DaffodilGirl 2012-11-16 03:06 采纳率: 0%
浏览 2214
已采纳

iPhone中解析XML遇到的问题

新手求助,遇到了解析XML的问题:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<Books>
    <Book id="1">
    <title_1>Circumference</title_1>
    <author>Nicholas Nicastro</author>
    <summary>Eratosthenes and the Ancient Quest to Measure the Globe.</summary>
</Book>
    <Book id="2">
    <title_1>Copernicus Secret</title_1>
    <author>Jack Repcheck</author>
    <summary>How the scientific revolution began</summary>
</Book>
    <Book id="3">
    <title_1>Angels and Demons</title_1>
    <author>Dan Brown</author>
    <summary>Robert Langdon is summoned to a Swiss research facility to analyze a cryptic symbol seared into the chest of a murdered physicist.</summary>
</Book>
    <Book id="4">
    <title_1>Keep the Aspidistra Flying</title_1>
    <author>George Orwell</author>
    <summary>A poignant and ultimately hopeful look at class and society, Keep the Aspidistra Flying pays tribute to the stubborn virtues of ordinary people who keep the aspidistra flying.</summary>
</Book>
</Books>

代码:

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

    if([elementName isEqualToString:@"Books"])
    {
        data_array = [[NSMutableArray alloc] init];
    }
    else if([elementName isEqualToString:@"Book"])
    {

        bookID= [[attributeDict objectForKey:@"id"]integerValue];

        NSLog(@"Reading id value :%d",bookID);


    //NSLog(@"Processing Element: %@",elementName);
    }

}

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


    if(!test_string)
    {
        test_string = [[NSMutableString alloc] initWithString:string];
}
    else
    {
        [test_string appendString:string];
    }
    //NSLog(@"Processing Value: %@",test_string);

}

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

    if([elementName isEqualToString:@"Books"])

        return;

    if ([elementName isEqualToString:@"Book"])
    {
        [data_array addObject:test_string];


    }
   else

    test_string = nil;
        }

实现之后就会跳到第一本书,并且只显示最后三本书。

如果加上test_string=nil就会显示作者的名字。

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

    if([elementName isEqualToString:@"Books"])

        return;

    if ([elementName isEqualToString:@"Book"])
    {
        [data_array addObject:test_string];

        test_string = nil;
        // data_array = nil;
    }

        }

而且在我的tableview中还会出现空格。

  • 写回答

1条回答

  • prettYYoyic 2012-11-16 07:07
    关注

    修改didStartElement方法:

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
     namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
        attributes:(NSDictionary *)attributeDict {  
    
        if([elementName isEqualToString:@"Books"])
        {
            data_array = [[NSMutableArray alloc] init];
        }
        else if([elementName isEqualToString:@"Book"])
        {
    
            bookID= [[attributeDict objectForKey:@"id"]integerValue];
            test_string = nil;
            NSLog(@"Reading id value :%d",bookID);
    
        }
        else if([elementName isEqualToString:@"title_1"])
        {
           test_string = [[NSMutableString alloc] init];
        }
    }
    

    再修改didEndElement:

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
      namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    
        if([elementName isEqualToString:@"Books"])
    
            return;
    
        if ([elementName isEqualToString:@"title_1"])
        {
            [data_array addObject:test_string];
    
            test_string = nil;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图