从http://www.cbr.ru/scripts/XML_daily.asp
检索xml请求时出现此错误这样的XML代码:
<?xml version="1.0" encoding="windows-1251" ?>
<ValCurs Date="01.03.2011" name="Foreign Currency Market">
<Valute ID="R01010">
<NumCode>036</NumCode>
<CharCode>AUD</CharCode>
<Nominal>1</Nominal>
<Name>Австралийский доллар</Name>
<Value>29,3508</Value>
</Valute>
<Valute ID="R01020A">
<NumCode>944</NumCode>
<CharCode>AZN</CharCode>
<Nominal>1</Nominal>
<Name>Азербайджанский манат</Name>
<Value>36,3374</Value>
</Valute>
我的代码是:
-(void)parserDidStartDocument:(NSXMLParser *)parser
{
[articles removeAllObjects];
}
-(void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
currentElement = elementName;
if ([elementName isEqualToString:@"ValCurs"]) {
itemActive = YES;
currentValute = [[NSMutableString alloc]init];
currentNumCode = [[NSMutableString alloc]init];
currentCharCode = [[NSMutableString alloc]init];
nominal = [[NSMutableString alloc]init];
currentName = [[NSMutableString alloc]init];
currentValue = [[NSMutableString alloc]init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (itemActive) {
NSString *fixedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([currentElement isEqualToString:@"Valute"])
[currentValute appendString:fixedString];
if ([currentElement isEqualToString:@"NumCode"])
[currentNumCode appendString:fixedString];
if ([currentElement isEqualToString:@"CharCode"])
[currentCharCode appendString:fixedString];
if ([currentElement isEqualToString:@"Nominal"])
[nominal appendString:fixedString];
if ([currentElement isEqualToString:@"Name"])
[currentName appendString:fixedString];
if ([currentElement isEqualToString:@"Value"])
[currentValue appendString:fixedString];
}
}
-(void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"ValCurs"]) {
NSDictionary *record = [NSDictionary dictionaryWithObjectsAndKeys:
currentValute,@"Valute",
currentNumCode,@"NumCode",
currentCharCode,@"CharCode",
nominal,@"nominal",
currentName,@"Name",
currentValue,@"Value",
nil];
[articles addObject:record];
[currentNumCode release];
[currentCharCode release];
[currentValute release];
[nominal release];
[currentName release];
[currentValue release];
itemActive = NO;
}
}
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"%@",articles);
//text.text = (@"%@",currentCharCode);
}
-(void)retrieveXML:(id)sender
{
[parser parse];
}
........
- (void)viewDidLoad {
[super viewDidLoad];
articles = [[NSMutableArray alloc]init];
NSString *myURL = [NSString stringWithFormat:@"http://www.cbr.ru/scripts/XML_daily.asp"];
NSURL *url = [NSURL URLWithString:myURL];
//NSString *urla = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
parser = [[NSXMLParser alloc]initWithContentsOfURL:url];
parser.delegate = self;
[self createUI];
}
来自NSXMLParser参考-NSXMLParserUnknownEncodingError = 31。 可能我需要使用编码来NSWindowsCP1251StringEncoding来解决问题吗?
答案 0 :(得分:1)
当文档编码未知时,会发生NSXMLParserUnknownEncodingError = 31。
试试这个:
NSString *myStr = [[NSString alloc] initWithData:myData encoding:NSWindowsCP1251StringEncoding];
myStr = [myStr stringByReplacingOccurrencesOfString:@"encoding=\"windows-1251\"" withString:@""];
NSData* aData = [myStr dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:aData];