NSXMLParser 中的奇怪符号

发布于 2024-08-21 19:55:33 字数 4847 浏览 7 评论 0原文

我被一个问题困住了;我尝试用 NSXMLParser 解析的所有内容都以“†Êá”(元素名称,elementText ...)结束

我尝试了不同的源(我想从我的服务器解析的源,简单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: †Êá

I'm stuck with a problem; everything I try to parse with the NSXMLParser ends up with "†Êá"(name of element, elementText ...)

I tried different sources (the one I'd like to parse from my server, simple NSString, and from different sources from the web which deliver XML) and every time "†Êá".

//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..

and the delegate methods

- (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); 
   }

i also tried different encodings such as ANSI and UTF8 .. but still I get the following results:

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: †Êá

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是一片海 2024-08-28 19:55:33

一个问题是您正在使用 NSASCIIStringEncoding。 XML 采用 UTF8 编码(如 XML 响应中所指定),因此您应该使用 NSUTF8StringEncoding

另一个问题是您在 NSLog 调用 NSString 对象中使用 %s,这是行不通的。 %s 用于 C 字符串、以 null 结尾的字符数组,而不是对象。将 %s 替换为 %@。

%@ 在 NSLog 中使用时,将在作为其匹配参数的任何对象上调用 description 方法。对于NSString的描述会打印出字符串的内容。

One problem is the fact that you're using NSASCIIStringEncoding. The XML is UTF8 encoded (as specified in the XML response), so you should be using NSUTF8StringEncoding.

Another the problem is that you're using %s in your NSLog calls for NSString Objects, which won't work. %s is for C strings, null terminated character arrays, not objects. Replace %s with %@.

%@ when used in NSLog will call the description method on any object that is its matching argument. For NSStrings description will print out the contents of the string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文