程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-iPhone中解析XML遇到的問題

ios-iPhone中解析XML遇到的問題

編輯:編程綜合問答
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中還會出現空格。

最佳回答:


修改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;
    }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved