2010-02-17 1 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에서개체를 호출 할 때 % s을 (를) 사용하고있어 문제가 발생한다는 것입니다. % s는 C 문자열, null 문자 배열, 객체가 아닙니다. % s을 % @로 바꿉니다. 에서 사용되는 경우 @ 일치하는 인수 인 모든 개체에서 description 메서드를 호출합니다. NSString s에 대한 설명은 문자열의 내용을 인쇄합니다.

+1

힌트를 보내 주셔서 감사합니다! 내가 인코딩과 아무 관계가 없다고 말했기 때문에 (내 excample에서 잘못된 네임을 사용 했음에도 불구하고 어떤 조합으로도 바 꾸었습니다.) 그러나 % s -> % @ 트릭 딕 it! 나는 지금 일주일 동안 만 objc를 사용하고 익숙하지 않습니다. 나는이 문제에 정말로 저를 몇 주간 구했다고 생각합니다 !! :디 –