使用 NSXMLParser 解析 XML 时出现问题
我在解析(显然)正确的 XML 代码时遇到了奇怪的问题!
解析的 xml 是:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<results>
<file id="0" name=" Linux Ubuntu.rar 700.64 2" size="700" disp="2"/>
<file id="1" name=" [Soft] Sistema operativo Linux. Live CD Distro Ubuntu-5.04-live-i386.iso 624.926 4" size="5" disp="4"/>
<file id="2" name=" ubuntu-9.04-server-i386.iso 577.220 2" size="9" disp="2"/>
<file id="3" name=" virtualbox-3.1_3.1.2-56127_Ubuntu_karmic_amd64.deb 43.578 1" size="3" disp="1"/>
<file id="4" name=" [APP-ITA].UBUNTU.LINUX.iso 586.822 2" size="586" disp="2"/>
<file id="5" name=" Ubuntu linux 2007.iso 700.446 1" size="700" disp="1"/>
<file id="6" name=" Installare aMule Adunanza + Liste Server + Liste Nodi su Ubuntu Gutsy [Fastweb 0.72 5" size="0" disp="5"/>
<file id="7" name=" - Guida Per Linux Ubuntu 7.03 Facile Da Usare!!!!!!!La Prima In Ita.rar 731.351 3" size="7" disp="3"/>
<file id="8" name=" Ubuntu Hacks - Tips and Tools for Exploring, Using, and Tuning Linux (O'Reilly, 3.494 1" size="3" disp="1"/>
<file id="9" name=" Linux-ubuntu-8.04.1-desktop-i386.iso 694.498 3" size="8" disp="3"/>
<file id="10" name=" [MANUALE] Ubuntu Linux - Computer Magazine.pdf 86.992 2" size="86" disp="2"/>
<file id="11" name=" (Ebook - Ita - Software) Ubuntu - Desktop Guide.pdf 0.686 3" size="0" disp="3"/>
<file id="12" name=" Installare Amule Adunanza In Ubuntu.rar 0.25 6" size="0" disp="6"/>
<file id="13" name=" UBUNTU LINUX [ITA].PDF 0.536 62" size="0" disp="62"/>
<file id="14" name=" Comandi Fondamentali Ubuntu.rtf 0.67 4" size="0" disp="4"/>
<file id="15" name=" ubuntu Guida.tar 0.160 1" size="0" disp="1"/>
<file id="16" name=" ubuntu-remix-italiano-8.10.iso 702.720 1" size="8" disp="1"/>
</results>
</root>
NSXMLParser 给了我以下错误:
2010-01-13 20:23:22.500 iMule[1419:20b] Error 65, Description: (null), Line: 13, Column: 24
2010-01-13 20:23:22.516 iMule[1419:20b] Error 4, Description: (null), Line: 1, Column: 1
有趣的是,如果我单独解析这些行,我没有问题,解析器根本不会大惊小怪!
我的解析代码是:
// DELEGATE XML PARSER
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
if([elementName isEqualToString:@"downloads"] || [elementName isEqualToString:@"results"]){
NSLog(@"starting or downloads or results");
if(xmlArray){
xmlArray= nil;
}
self.xmlArray= [[NSMutableArray alloc] init];
if([elementName isEqualToString:@"results"]){
[self.results_controller.activity startAnimating];
}
if([elementName isEqualToString:@"downloads"]){
[self.downloads_Controller.activity startAnimating];
}
}
else if([elementName isEqualToString:@"file"]){
NSLog(@"found file...");
[self.xmlArray addObject:attributeDict];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(elementName);
if([elementName isEqualToString:@"downloads"] || [elementName isEqualToString:@"results"]){
if([elementName isEqualToString:@"downloads"]){
NSLog(@"downloads found... reloading table");
self.downloads_Controller.downloads= xmlArray;
[self.downloads_Controller.tableView reloadData];
[self.downloads_Controller.activity stopAnimating];
}
else if([elementName isEqualToString:@"results"]){
NSLog(@"results found... reloading table");
self.results_controller.results= xmlArray;
// NSLog(@"xmlarray: %@ and results: %@", xmlArray, self.results_controller.results);
[self.results_controller.tableView reloadData];
[self.results_controller.activity stopAnimating];
}
}
else if([elementName isEqualToString:@"error"]){
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"Error" message:@"aMule dosent seem to be on" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
NSLog(@"Error %i, Description: %@, Line: %i, Column: %i", [parseError code],
[[parser parserError] localizedDescription], [parser lineNumber],
[parser columnNumber]);
}
- (void)parser:(NSXMLParser *)parser validationErrorOccurred:(NSError *)validError{
NSLog(@"valid: %@", validError);
}
// END DELEGATES XML PARSER
有人知道它可能是什么吗?
谢谢
I'm having strange problems parsing (apparently) correct XML code!
The xml parsed is:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<results>
<file id="0" name=" Linux Ubuntu.rar 700.64 2" size="700" disp="2"/>
<file id="1" name=" [Soft] Sistema operativo Linux. Live CD Distro Ubuntu-5.04-live-i386.iso 624.926 4" size="5" disp="4"/>
<file id="2" name=" ubuntu-9.04-server-i386.iso 577.220 2" size="9" disp="2"/>
<file id="3" name=" virtualbox-3.1_3.1.2-56127_Ubuntu_karmic_amd64.deb 43.578 1" size="3" disp="1"/>
<file id="4" name=" [APP-ITA].UBUNTU.LINUX.iso 586.822 2" size="586" disp="2"/>
<file id="5" name=" Ubuntu linux 2007.iso 700.446 1" size="700" disp="1"/>
<file id="6" name=" Installare aMule Adunanza + Liste Server + Liste Nodi su Ubuntu Gutsy [Fastweb 0.72 5" size="0" disp="5"/>
<file id="7" name=" - Guida Per Linux Ubuntu 7.03 Facile Da Usare!!!!!!!La Prima In Ita.rar 731.351 3" size="7" disp="3"/>
<file id="8" name=" Ubuntu Hacks - Tips and Tools for Exploring, Using, and Tuning Linux (O'Reilly, 3.494 1" size="3" disp="1"/>
<file id="9" name=" Linux-ubuntu-8.04.1-desktop-i386.iso 694.498 3" size="8" disp="3"/>
<file id="10" name=" [MANUALE] Ubuntu Linux - Computer Magazine.pdf 86.992 2" size="86" disp="2"/>
<file id="11" name=" (Ebook - Ita - Software) Ubuntu - Desktop Guide.pdf 0.686 3" size="0" disp="3"/>
<file id="12" name=" Installare Amule Adunanza In Ubuntu.rar 0.25 6" size="0" disp="6"/>
<file id="13" name=" UBUNTU LINUX [ITA].PDF 0.536 62" size="0" disp="62"/>
<file id="14" name=" Comandi Fondamentali Ubuntu.rtf 0.67 4" size="0" disp="4"/>
<file id="15" name=" ubuntu Guida.tar 0.160 1" size="0" disp="1"/>
<file id="16" name=" ubuntu-remix-italiano-8.10.iso 702.720 1" size="8" disp="1"/>
</results>
</root>
NSXMLParser gives me the following error:
2010-01-13 20:23:22.500 iMule[1419:20b] Error 65, Description: (null), Line: 13, Column: 24
2010-01-13 20:23:22.516 iMule[1419:20b] Error 4, Description: (null), Line: 1, Column: 1
The funny thing is that, if i parse these lines singularly i have no problems, the parser dosent fuss at all!
My parsing code is:
// DELEGATE XML PARSER
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
if([elementName isEqualToString:@"downloads"] || [elementName isEqualToString:@"results"]){
NSLog(@"starting or downloads or results");
if(xmlArray){
xmlArray= nil;
}
self.xmlArray= [[NSMutableArray alloc] init];
if([elementName isEqualToString:@"results"]){
[self.results_controller.activity startAnimating];
}
if([elementName isEqualToString:@"downloads"]){
[self.downloads_Controller.activity startAnimating];
}
}
else if([elementName isEqualToString:@"file"]){
NSLog(@"found file...");
[self.xmlArray addObject:attributeDict];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(elementName);
if([elementName isEqualToString:@"downloads"] || [elementName isEqualToString:@"results"]){
if([elementName isEqualToString:@"downloads"]){
NSLog(@"downloads found... reloading table");
self.downloads_Controller.downloads= xmlArray;
[self.downloads_Controller.tableView reloadData];
[self.downloads_Controller.activity stopAnimating];
}
else if([elementName isEqualToString:@"results"]){
NSLog(@"results found... reloading table");
self.results_controller.results= xmlArray;
// NSLog(@"xmlarray: %@ and results: %@", xmlArray, self.results_controller.results);
[self.results_controller.tableView reloadData];
[self.results_controller.activity stopAnimating];
}
}
else if([elementName isEqualToString:@"error"]){
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"Error" message:@"aMule dosent seem to be on" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
NSLog(@"Error %i, Description: %@, Line: %i, Column: %i", [parseError code],
[[parser parserError] localizedDescription], [parser lineNumber],
[parser columnNumber]);
}
- (void)parser:(NSXMLParser *)parser validationErrorOccurred:(NSError *)validError{
NSLog(@"valid: %@", validError);
}
// END DELEGATES XML PARSER
Does someone have a clue of what it could be?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当你说“单独解析这些行”时,你的意思是你给了它一个完整的 XML 文档,只有一个“文件”条目?如果是这种情况,那么另一种方法是使用两个“文件”条目设置 XML 文档,一个包含文件 0 和文件 1,一个包含文件 1 和文件 2,一个包含文件 2 和文件 3,等等,并且看看所有对是否都能正常工作。根据将 XML 传递到解析器的方式,您可能会遇到仅在解析连续文件条目时才会出现的问题。
根据您的解析代码的长度,将其添加到原始帖子中会对您有所帮助。
这个简短的程序可以使用 NSXMLParser 很好地解析您提供的数据:
When you say "parse these lines singularly" do you mean you handed it a complete XML document with only a single "file" entry? If that's the case, then another approach would be to set up the XML document with two "file" entries, one with file 0 and file 1, one with file 1 and file 2, one with file 2 and file3, etc., and see if all pairs work correctly. Depending on how you're passing the XML into the parser, you may have a problem that only shows up when parsing successive file entries.
Depending on how long your parsing code is, it would be helpful for you to add it to the original post.
This short program parses your supplied data just fine with NSXMLParser:
你查过错误代码吗?
NSXMLParserSpaceRequiredError
= 65,
NSXMLParserInternalError
= 1,
看起来 XML 解析器认为第 13 行第 24 列需要一些空间。
您能否将 XML 数据发布到 Pastebin 上,以便我们查看原始数据?我怀疑您或 Stack Overflow 以某种方式更改了数据(可能是出于格式原因),这导致 NSXMLParser 报告的行/列号丢失。
Did you look up the error codes?
NSXMLParserSpaceRequiredError
= 65,
NSXMLParserInternalError
= 1,
Looks like the XML parser thinks some space is required at the 24th column of the 13th line.
Could you post the XML data on a pastebin so we can look at the raw data? I suspect that either you or Stack Overflow changed the data in some way (perhaps for formatting reasons) and this threw off the line/column numbers from what NSXMLParser is reporting.