1

XCode を使用して非常に単純な XML ドキュメントを解析しています。うまく動作する場合もあれば、次のように返される場合もあります。

document.cookie='llllll=9bb65648llllllll_9bb65648; path=/';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");
}

}

私はかなり困惑していますが、私には見えない単純なものに違いないと確信しています。任意のヒント?

4

1 に答える 1

1

解析しようとしている応答は (おそらく) XML 形式ではありません。

xml 応答を NSLog し、サーバーが奇妙なコードを返したときに何が返されるかを確認します。xml を解析する前に検証します。

于 2013-01-27T17:37:13.403 に答える