2013-05-27 140 views
0

這是我的countries.xml文件。解析XML時出錯「解析文檔錯誤!」

有什麼問題請回答我,我是新來的iOS .....

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict/> 
<countris> 
<country> 
    <countryname>india<countryname>  
</country> 
</countris> 
</plist> 

這是我viewDidLoad

NSString *filename = [[NSBundle mainBundle] pathForResource:@"countris" ofType:@"xml"]; 
NSData *data = [NSData dataWithContentsOfFile:filename]; 


NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; 

Myparser *myParser = [[Myparser alloc] init]; 

[parser setDelegate:myParser]; 

BOOL success = [parser parse]; 

if (success) 
{ 
    NSMutableArray *users = [myParser arraydata]; 
    NSString *log= [NSString stringWithFormat: 
          @"This is an array of User objects:\n\n%@", 
          [users description]]; 
    NSLog(log); 
} 
else 
{ 
    NSLog(@"Error parsing document!"); 

    [[[UIAlertView alloc] initWithTitle:nil 
           message:@"Error parsing document!" 
           delegate:nil 
         cancelButtonTitle:@"OK" 
         otherButtonTitles:nil] show]; 
} 

我Myparser.m文件,其中我的implemantig我NSXMLParserDelegate協議

#import "Myparser.h" 
#import "MyParserData.h" 

@implementation Myparser 

@synthesize mydata,myparser,arraydata,flag,currentElementValue; 

- (id) init { 

    if(self = [super init]) { 
     currentElementValue = [[NSMutableString alloc] initWithString:@""]; 
     arraydata = [[NSMutableArray alloc] init]; 
     flag = FALSE; 
    } 
    return self; 
} 

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

    if ([elementName isEqualToString:@"countris"]) { 
     return; 
    } else if ([elementName isEqualToString:@"country"]) { 
     self.mydata = [[MyParserData alloc] init]; 
     } else { 
     self.currentElementValue = [[NSMutableString alloc] init]; 
    } 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
    if (self.currentElementValue) { 
     [self.currentElementValue appendString:string]; 
    } 
} 

- (void)parser:(NSXMLParser *)parserdidEndElement:(NSString *)elementNamenamespaceURI:   (NSString *)namespaceURIqualifiedName:(NSString *)qName { 

    if ([elementName isEqualToString:@"counteis"]) { 

     return; 
    } 

    if ([elementName isEqualToString:@"country"]) { 
     [self.arraydata addObject:mydata]; 
     self.mydata = nil; 
    } else { 
     [self.mydata setValue:self.currentElementValue forKey:elementName]; 
    } 

    self.currentElementValue = nil; 
} 

@end 
+0

你的問題是什麼?如果你沒有指明你的問題,我們該如何解決? – manujmv

+0

@manujmv我認爲問題在那裏的問題標題 –

+0

感謝您的幫助我git的問題... –

回答

1

你的XML是不是合法的解析器的內存。您有一個<countryname>標記,但您永遠不會關閉它(即,您在</countryname>中缺少/)。

當使用NSXMLParser時,我會建議實施parseErrorOccurred,然後查看錯誤對象,並且它通常會讓您瞭解在XML源代碼中查找問題的位置。 Cocoa框架通常非常擅長提供獲取錯誤信息的方法,因此可以利用這些技術。


順便說一句,我注意到,你似乎是試圖用一個plist格式,但它並不完全符合正確的格式,如果你手寫的XML。通常,如果您使用plist文件,則根本不需要使用NSXMLParser(也不會手動創建XML源)。

想象一下下面的源代碼:

NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; 
NSString *filename = [docsPath stringByAppendingPathComponent:@"test.plist"]; 

NSDictionary *dictionary = @{@"countries": @{@"country" : @"india"}}; 
[dictionary writeToFile:filename atomically:YES]; 

這將創建一個test.plist文件看起來像:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>countries</key> 
    <dict> 
     <key>country</key> 
     <string>india</string> 
    </dict> 
</dict> 
</plist> 

的事情是,一旦你創建一個使用writeToFile一個plist中,您現在可以讀取它作爲:

dictionary = [NSDictionary dictionaryWithContentsOfFile:filename]; 
NSLog(@"dictionary = %@", dictionary); 

顯然,如果你想學習如何u se NSXMLParser,繼續你的實驗。但是當使用plist文件時,可以通過僅使用writeToFile保存和dictionaryWithContentsOfFile加載來簡化該過程。

順便說一下,這個過程不僅適用於NSDictionary對象,也適用於NSArray對象。例如,考慮這一陣列詞典:

// create array plist 

filename = [docsPath stringByAppendingPathComponent:@"array.plist"]; 
NSArray *array = @[@{@"country" : @"india"}, @{@"country" : @"france"}]; 
[array writeToFile:filename atomically:YES]; 

// read array plist 

array = [NSArray arrayWithContentsOfFile:filename]; 

但這個想法是,你可以保存字典和數組中的XML格式,無需自己接觸過的XML源,也不必處理NSXMLParser。請參閱Property List Programming Guide


而且,如果你的數組和字典包含自定義對象,即比標準可可對象的其他NS對象(如NSStringNSNumberNSDate等),有一個名爲NSKeyedArchiver另一種技術和NSKeyedUnarchiver,讓你再次使用XML格式編寫二進制plist文件,但是又一次避免了你自己寫XML源的細節,也沒有處理NSXMLParser。這更復雜一點,除非你使用自定義類來處理數組/字典,否則我不會擔心它,但它是另一個很好的抽象,它可以處理XML文件,而無需處理XML本身。

+0

感謝@rob這幫了我很多 –

1

看來,在您的countries.xml文件中,您並未關閉countryn ame,但在一個國家的實際名稱後再打開它。

+0

thabks但我的日誌輸出給我「」 –

2
WRONG <countryname>india<countryname>  
RIGHT <countryname>india</countryname>  

這會在解析器中導致錯誤,因此它會觸發錯誤委託方法。

輸出記錄其中有無關的結果

+0

我怎麼能得到數據? –