我正在用 XCode 解析一个非常简单的 XML 文档。有时它工作得很好,其他时候它返回这个:
document.cookie='llllll=9bb65648llllllll_9bb65648; 路径=/';window.location.href=window.location.href;
这是我的头文件:
@interface NewsViewController : UIViewController{
NSXMLParser *rssParser;
NSMutableArray *articles;
NSMutableDictionary *item;
NSString *currentElement;
NSMutableString *ElementValue;
BOOL errorParsing;
}
@property (weak, nonatomic) IBOutlet UILabel *newsText;
@property (retain) NSMutableString *theString;
- (void)parseXMLFileAtURL:(NSString *)URL;
@end
这是我的实现代码:
-(void)viewDidLoad{
[self parseXMLFileAtURL:@"http://www.domain.com/news.xml"];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(@"File found and parsing started");
}
- (void)parseXMLFileAtURL:(NSString *)URL
{
NSString *agentString = @"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; en-us) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:URL]];
[request setValue:agentString forHTTPHeaderField:@"User-Agent"];
NSData *xmlFile = [ NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ];
articles = [[NSMutableArray alloc] init];
errorParsing=NO;
rssParser = [[NSXMLParser alloc] initWithData:xmlFile];
[rssParser setDelegate:self];
// You may need to turn some of these on depending on the type of XML file you are parsing
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString *errorString = [NSString stringWithFormat:@"Error code %i", [parseError code]];
NSLog(@"Error parsing XML: %@", errorString);
errorParsing=YES;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
ElementValue = [[NSMutableString alloc] init];
if ([elementName isEqualToString:@"item"]) {
item = [[NSMutableDictionary alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
[ElementValue appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"item"]) {
[articles addObject:[item copy]];
} else {
[item setObject:ElementValue forKey:elementName];
NSLog (@"--%@",ElementValue);
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
if (errorParsing == NO)
{
NSLog(@"XML processing done!");
theString=[NSString stringWithFormat:@"%@",ElementValue];
NSLog (@"Parsed: %@",theString);
} else {
NSLog(@"Error occurred during XML processing");
}
}
我很困惑,但我敢肯定这一定是一些我看不到的简单事情。有小费吗?