【问题标题】:parsing xml file with spanish (non UTF-8 format)用西班牙语解析 xml 文件(非 UTF-8 格式)
【发布时间】:2012-11-30 10:45:32
【问题描述】:

我需要用西班牙语解析 xml 文件(如果我无法控制生成方式)。解析部分工作得很好,但问题是当 xml 文件有特殊字符时,例如:

看点

什么时候解析我得到这个: 点睛之笔

我正在使用 CocoaXMLParser。你们有谁知道这是怎么处理的?

这是我的代码:

-(void)getRss
{
    NSString *urlString=@"http://mysite.com/content.xml";
    NSURL *url=[NSURL URLWithString:urlString];
    NSURLRequest *rssRequest=[NSURLRequest requestWithURL:url];
    self.contentConnection=[[NSURLConnection alloc]initWithRequest:rssRequest delegate:self startImmediately:YES];


}




- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {


    self.dataResponse = [NSMutableData data];

    NSLog(@"didReceiveResponse");

}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [_dataResponse appendData:data];

     NSLog(@"didReceiveData");




}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    NSLog(@"didFailWithError");

}



- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

     NSLog(@"connectionDidFinishLoading ");

     [self parseContent];
}


-(void)parseContent
{
    NSString *response = [[NSString alloc] initWithData:_dataResponse encoding:NSUTF8StringEncoding];
    NSLog(@"data received %@", response);
    NSLog(@"parse content ");

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:_dataResponse];
    parser.delegate = self;
    [parser parse];


}


- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    self.currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementname isEqualToString:@"categoriaNoticias"])
    {
            self.validXML=YES;
        NSLog(@"es xml valido");

    }
    else
    {
         self.validXML=YES;
    }
}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if (_validXML) {
        if ([elementname isEqualToString:@"titulo"])
        {
            NSString *string=_currentNodeContent;
            NSLog(@"titulo %@", string);
        }
        if ([elementname isEqualToString:@"link"])
        {
        NSLog(@"titulo %@", _currentNodeContent);
    }

}

}

我会非常感谢任何指点

【问题讨论】:

  • 按照标准,XML 必须以 UTF-8 编码。
  • @mvp:“XML 文档中的每个外部解析实体可能对其字符使用不同的编码。”来自w3.org/TR/REC-xml/#charencoding
  • 我明白,但正如我提到的,我无法控制 xml 文件的创建,我需要与我一起工作
  • @Juan:文档有一些编码,可能是 latin-1。如果有错误或遗漏,也许在您阅读后补上?
  • @Juan:你的 XML 文件有什么编码?您如何接收它并将其提供给 XML 解析器?请出示相关代码。

标签: iphone ios ipad xml-parsing


【解决方案1】:

假设您的 XML 文件以 Latin-1 (ISO-8859-1) 编码,您可以即时修复 XML 文件:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    const char* xmlDecl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\r\n";
    self.dataResponse = [NSMutableData data];
    [self.dataResponse appendBytes: xmlDecl length: strlen(xmlDecl)];
}

请检查有效编码是什么,并根据需要进行相应调整。

【讨论】:

  • 我已经添加了上面的代码,但现在我得到了: 什么都没有被解析。
  • 那么您的 XML 文件不以 &lt;rss version=... 开头,正如您在上面的评论中所写的那样。最后你需要弄清楚有效的编码是什么。
  • @Juan 使用它来找出响应 [[response allHeaderFields] objectForKey:@"Content-Type"] 的内容类型,对于 UTF-8 等,该值将是 text/html; charset=utf-8
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-01
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 2013-07-14
相关资源
最近更新 更多