开发者

NSXMLParser parsing problem on PHP-generated XML

开发者 https://www.devze.com 2023-02-15 15:43 出处:网络
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 th

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 开发者_StackOverflowparseErrorOccured:(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 ......
?>


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')
0

精彩评论

暂无评论...
验证码 换一张
取 消