2010-02-17 49 views
0

我被一個問題困住了;我試圖用NSXMLParser解析的所有內容都以「†Êá」(元素名稱,elementText ...)結尾來自NSXMLParser的奇怪符號

我嘗試了不同的來源(我想從我的服務器解析的那個,簡單的NSString和來自提供XML的網絡的不同來源)以及每一次「†Êá」。

//prepar request 
//NSString *urlString = [NSString stringWithFormat:@"http://192.168.1.102:8080/support/supportService"]; 
NSString *urlString = [NSString stringWithFormat:@"http://sites.google.com/site/iphonesdktutorials/xml/Books.xml"]; 
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:urlString]]; 
[request setHTTPMethod:@"POST"]; 

//get response 
NSHTTPURLResponse* urlResponse = nil; 
NSError *error = [[NSError alloc] init]; 
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; 
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]; 
NSLog(@"Response Code: %d", [urlResponse statusCode]); 
//if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) { 
NSLog(@"Response: %@", result); 
// responsecode here 
//} 

NSXMLParser *parser = [[NSXMLParser alloc] initWithData: [ result dataUsingEncoding:NSASCIIStringEncoding]]; 
//NSXMLParser *parser = [[NSXMLParser alloc] initWithData: [ fakeResponse dataUsingEncoding:NSUTF8StringEncoding]]; 

[parser setDelegate:self]; // The parser calls methods in this class 
//[parser setShouldProcessNamespaces:NO]; // We don't care about namespaces 
//[parser setShouldReportNamespacePrefixes:NO]; // 
//[parser setShouldResolveExternalEntities:NO]; // We just want data, no other stuff 

[parser parse]; // Parse that data.. 

和委託方法

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 
     NSLog(@"chars: %s", string); 
    } 

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


    NSLog(@"open: %s %s, %s", elementName, namespaceURI, qName); 
    } 

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


    NSLog(@"close: %s", elementName); 
    } 

我也嘗試過不同的編碼,如ANSI和UTF8 ..但我仍然得到以下結果:

2010-02-17 09:29:18.377 SupportServiceWSTest[403:20b] Response Code: 200 
2010-02-17 09:29:18.378 SupportServiceWSTest[403:20b] Response: <?xml version="1.0" 
encoding="UTF-8"?><Books><Book id="1"><title>Circumference</title><author>Nicholas 
Nicastro</author><summary>Eratosthenes and the Ancient Quest to Measure the 
Globe.</summary></Book><Book id="2"><title>Copernicus Secret</title><author>Jack 
Repcheck</author><summary>How the scientific revolution began</summary></Book><Book 
id="3"><title>Angels and Demons</title><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>Keep the 
Aspidistra Flying</title><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> 
2010-02-17 09:29:18.379 SupportServiceWSTest[403:20b] open: †Êá (null), (null) 
2010-02-17 09:29:18.381 SupportServiceWSTest[403:20b] open: †Êá (null), (null) 
2010-02-17 09:29:18.381 SupportServiceWSTest[403:20b] open: †Êá (null), (null) 
2010-02-17 09:29:18.382 SupportServiceWSTest[403:20b] chars: †Êá 
2010-02-17 09:29:18.382 SupportServiceWSTest[403:20b] close: †Êá 
2010-02-17 09:29:18.383 SupportServiceWSTest[403:20b] open: †Êá (null), (null) 
2010-02-17 09:29:18.383 SupportServiceWSTest[403:20b] chars: †Êá 
2010-02-17 09:29:18.384 SupportServiceWSTest[403:20b] close: †Êá 
2010-02-17 09:29:18.384 SupportServiceWSTest[403:20b] open: †Êá (null), (null) 
2010-02-17 09:29:18.385 SupportServiceWSTest[403:20b] chars: †Êá 
2010-02-17 09:29:18.385 SupportServiceWSTest[403:20b] close: †Êá 
2010-02-17 09:29:18.386 SupportServiceWSTest[403:20b] close: †Êá 
2010-02-17 09:29:18.386 SupportServiceWSTest[403:20b] open: †Êá (null), (null) 
2010-02-17 09:29:18.387 SupportServiceWSTest[403:20b] open: †Êá (null), (null) 
2010-02-17 09:29:18.387 SupportServiceWSTest[403:20b] chars: †Êá 
2010-02-17 09:29:18.388 SupportServiceWSTest[403:20b] close: †Êá 
2010-02-17 09:29:18.388 SupportServiceWSTest[403:20b] open: †Êá (null), (null) 
2010-02-17 09:29:18.389 SupportServiceWSTest[403:20b] chars: †Êá 

回答

2

的一個問題是事實你正在使用NSASCIIStringEncoding。 XML是UTF8編碼(如XML響應中所述),所以您應該使用NSUTF8StringEncoding

另一個問題是,您在NSLog調用NSString對象時使用%s,這不起作用。 %s是C字符串,空終止的字符數組,而不是對象。用%@替換%s。

%@在NSLog中使用時,將在任何與其匹配參數的對象上調用description方法。對於NSString的描述將打印出字符串的內容。

+1

謝謝你的提示!正如我所說的,它與編碼無關(儘管我在這裏使用了一個錯誤的ne,我把它改爲任何組合) 但是%s - >%@ trick dit it!即時使用OBJC只有一個星期了,我不熟悉它。我認爲你真的節省了我幾周的時間! :d – 2010-02-17 10:34:24