NSXMLParser 对 PHP 生成的 XML 的解析问题

发布于 2024-10-20 13:20:18 字数 3290 浏览 2 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凶凌 2024-10-27 13:20:18

如果您已设法使其与服务器上的 XML 文件正常工作,那么问题很可能出在 PHP 上。检查您是否将响应标头内容类型设置为 application/xml 或 text/xml。

header('Content-Type: 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.

header('Content-Type: text/xml')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文