解析XML有时会返回奇怪的代码

时间:2013-01-27 17:28:14

标签: ios xml xcode xml-parsing

我正在使用XCode解析一个非常简单的XML文档。有时它可以正常工作,有时它会返回:

的document.cookie ='lllllll = 9bb65648lllllll_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");
}

}

我很难过,但我确信它一定是简单的我看不到的东西。有什么提示吗?

1 个答案:

答案 0 :(得分:1)

您尝试解析的响应(可能)不是XML格式。

NSLog您的xml响应,并查看服务器返回奇怪代码时返回的内容。在解析之前验证xml。