NSXMLParser 对 PHP 生成的 XML 的解析问题
我使用 NSXMLParser 制作了一个解析器 iPhone 应用程序来解析 PHP 生成的 XML (http://raptor.hk/rta_handler.php?action=category),但没有返回结果。我尝试了一些方法,但仍然无法返回结果。
我不小心将 PHP 生成的 XML 保存到 XML 文件并重新上传到 Web 服务器(http://raptor.hk/rta_handler.xml),并更改了 NSXMLParser,解析器应用程序可以工作并返回我需要的所有内容。
问题是,我需要 PHP 函数来使 XML 动态化。我怎样才能实现它?我的 NSXMLParser XML 代码如下:
在加载解析器函数中:
NSURL *xmlURL = [NSURL URLWithString:URL];
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
在 (void)parserDidStartDocument:(NSXMLParser *)parser
中,
在 (void)parser:( 中 为空NSXMLParser *)parser parseErrorOccured:(NSError *)parseError
NSString *errorString = [NSString stringWithFormat:@"Unable to download entries (ERR_CODE: %i)", [parseError code]];
NSLog(@"error parsing XML: %@", errorString);
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
errorAlert = nil;
in (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI QualifiedName:(NSString *)qName属性:(NSDictionary *)attributeDict
,
currentElement = [elementName copy];
if ([elementName isEqualToString:@"category"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
self.currentTitle = [[NSMutableString alloc] init];
self.currentID = 0;
}
在 (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI QualifiedName:(NSString *)qName
,
if ([elementName isEqualToString:@"category"]) {
[item setObject:self.currentTitle forKey:@"title"];
[item setObject:[NSString stringWithFormat:@"%d", self.currentID] forKey:@"id"];
[self.entries addObject:item];
[item release];
item = nil;
}
在 (void)parser:(NSXMLParser *)parserfoundCharacters:(NSString *)string
中,
NSString *trimmed = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([currentElement isEqualToString:@"title"]) {
[self.currentTitle appendString:trimmed];
} else if([currentElement isEqualToString:@"id"]) {
if(trimmed.length > 0) {
self.currentID = [trimmed intValue];
}
}
在 (void)parserDidEndDocument:(NSXMLParser *)parser
中,
[tbl_categories reloadData];
在 >(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
,一些不相关的实验代码
那么,我的代码有什么问题吗?或者在我的 PHP 生成的 XML 中?
更新:我使用的 PHP 代码
<?php
require_once('wp-blog-header.php');
// === XML Header ===
header('Content-Type: text/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="utf-8" ?>' . "\n";
// echo the XML contents here ......
?>
I made a parser iPhone app using NSXMLParser to parse a PHP-generated XML (http://raptor.hk/rta_handler.php?action=category), but no result returned. I tried a few methods, but still, I can't make the results returned.
I accidentally saved the PHP-generated XML to a XML file and re-upload to web server (http://raptor.hk/rta_handler.xml), and changed the NSXMLParser, the parser app works and returns everything I need.
The problem is, I need PHP functions to make the XML dynamic. How can I achieve it? My NSXMLParser
XML codes are as follow:
in load parser function :
NSURL *xmlURL = [NSURL URLWithString:URL];
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
in (void)parserDidStartDocument:(NSXMLParser *)parser
, empty
in (void)parser:(NSXMLParser *)parser parseErrorOccured:(NSError *)parseError
NSString *errorString = [NSString stringWithFormat:@"Unable to download entries (ERR_CODE: %i)", [parseError code]];
NSLog(@"error parsing XML: %@", errorString);
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
errorAlert = nil;
in (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
,
currentElement = [elementName copy];
if ([elementName isEqualToString:@"category"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
self.currentTitle = [[NSMutableString alloc] init];
self.currentID = 0;
}
in (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
,
if ([elementName isEqualToString:@"category"]) {
[item setObject:self.currentTitle forKey:@"title"];
[item setObject:[NSString stringWithFormat:@"%d", self.currentID] forKey:@"id"];
[self.entries addObject:item];
[item release];
item = nil;
}
in (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
,
NSString *trimmed = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([currentElement isEqualToString:@"title"]) {
[self.currentTitle appendString:trimmed];
} else if([currentElement isEqualToString:@"id"]) {
if(trimmed.length > 0) {
self.currentID = [trimmed intValue];
}
}
in (void)parserDidEndDocument:(NSXMLParser *)parser
,
[tbl_categories reloadData];
in (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
, some unrelated experimental codes
So, is there any problem in my code? or in my PHP-generated XML?
UPDATE: PHP code I used
<?php
require_once('wp-blog-header.php');
// === XML Header ===
header('Content-Type: text/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="utf-8" ?>' . "\n";
// echo the XML contents here ......
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您已设法使其与服务器上的 XML 文件正常工作,那么问题很可能出在 PHP 上。检查您是否将响应标头内容类型设置为 application/xml 或 text/xml。
If you've managed to get it working correctly with an XML file on the server then the problem is most likely with the PHP. Check that you are setting the response header content-type to application/xml or text/xml.