NSXMLParser 替换 é带有 \U00e9 的字符
我正在使用XML解析器,nsxmlparser
在nsmutablearray
中解析a xml asn xml返回某些url
。 法语
被\ u00e9
取代。
这是我的代码:
- (void)parseXMLFileAtURL:(NSString *)URL
{
NSURL *xmlURL = [NSURL URLWithString:URL];
xmlParser = [NSXMLParser alloc] initWithContentsOfURL:xmlURL];
// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[xmlParser setDelegate:self];
// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[xmlParser setShouldProcessNamespaces:NO];
[xmlParser setShouldReportNamespacePrefixes:NO];
[xmlParser setShouldResolveExternalEntities:NO];
[xmlParser parse];
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:@"catalogue"]) {
// clear out our story item caches...
currentCatalogue = [[Catalogue alloc] init];
}
if ([elementName isEqualToString:@"partenaire"]) {
// clear out our story item caches...
currentPartenaire = [[Partenaire alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"catalogue"]) {
// Add currentCatalogue to array
[catalogueList addObject: currentCatalogue];
NSString *urls=[catalogueList valueForKey:@"url"];
NSLog(@"Current catalogue: urls=%@", urls);
}
if ([elementName isEqualToString:@"partenaire"]) {
// Add currentPartenaire to array
[partenaireList addObject: currentPartenaire];
/*NSLog(@"Current partenaire: raison_sociale=%@, lat=%@, lng=%@", currentPartenaire.raison_sociale, currentPartenaire.lat, currentPartenaire.lng);*/
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
// Catalogue setup
if ([currentElement isEqualToString:@"id_model"])
currentCatalogue.id_model = string;
if ([currentElement isEqualToString:@"url"])
{
if (currentCatalogue.url)
{
currentCatalogue.url = [NSString stringWithFormat: @"%@%@", currentCatalogue.url, string];
// NSLog(@"Valoare url in data handler %@", currentCatalogue.url);
}
else
currentCatalogue.url = string;
}
}
有人知道如何解决这个问题吗?
I'm using an xml parser, NSXMLParser
to parse asn xml an return some url
in an NSMutableArray
.Everything is giong great except the fact that the french é
is replaced by \U00e9
.
Here is my code:
- (void)parseXMLFileAtURL:(NSString *)URL
{
NSURL *xmlURL = [NSURL URLWithString:URL];
xmlParser = [NSXMLParser alloc] initWithContentsOfURL:xmlURL];
// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[xmlParser setDelegate:self];
// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[xmlParser setShouldProcessNamespaces:NO];
[xmlParser setShouldReportNamespacePrefixes:NO];
[xmlParser setShouldResolveExternalEntities:NO];
[xmlParser parse];
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:@"catalogue"]) {
// clear out our story item caches...
currentCatalogue = [[Catalogue alloc] init];
}
if ([elementName isEqualToString:@"partenaire"]) {
// clear out our story item caches...
currentPartenaire = [[Partenaire alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"catalogue"]) {
// Add currentCatalogue to array
[catalogueList addObject: currentCatalogue];
NSString *urls=[catalogueList valueForKey:@"url"];
NSLog(@"Current catalogue: urls=%@", urls);
}
if ([elementName isEqualToString:@"partenaire"]) {
// Add currentPartenaire to array
[partenaireList addObject: currentPartenaire];
/*NSLog(@"Current partenaire: raison_sociale=%@, lat=%@, lng=%@", currentPartenaire.raison_sociale, currentPartenaire.lat, currentPartenaire.lng);*/
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
// Catalogue setup
if ([currentElement isEqualToString:@"id_model"])
currentCatalogue.id_model = string;
if ([currentElement isEqualToString:@"url"])
{
if (currentCatalogue.url)
{
currentCatalogue.url = [NSString stringWithFormat: @"%@%@", currentCatalogue.url, string];
// NSLog(@"Valoare url in data handler %@", currentCatalogue.url);
}
else
currentCatalogue.url = string;
}
}
Anyone any idea how to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
测试的代码:100%工作
输出:
TESTED CODE : 100 % WORKS
OUTPUT:
假设您的XML具有明确的编码,则如下:
然后您应该首先解决两个问题。这些可能会或可能不会解决您的直接问题,但是如果没有,它们将使您在看到的问题上更容易缩小。这是您应该解决的问题:
尝试使用这些修复程序,看看它是否有效。如果没有,请使用代码的更正版本更新您的帖子,这可能是什么问题。
Assuming your XML has an explicit encoding, as follows:
then there are 2 problems you should address first. These may or may not fix your direct problem, but if not they will make it easier to narrow in on the problem you are seeing. Here are the problems you should fix:
Try it out with these fixes and see if it works. If not, update your post with a corrected version of your code and it might be more obvious what the problem is.