简体   繁体   中英

XML(in php) parsing on iPhone

I am developing an iPhone app and I want to put some datas to UITableView. I got an app showing several xml parser run from here .

and then, I'd separated GDataXMLParser from this project and made it run but I have an odd problem that I can't figure out.

This is the code putting php file.

- (void)start {
    self.startTimeReference = [NSDate timeIntervalSinceReferenceDate];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    self.parsedSongs = [NSMutableArray array];
    NSURL *url = [NSURL URLWithString:@"http://mydomain.blabla/phpxmltest.php"];
    [NSThread detachNewThreadSelector:@selector(downloadAndParse:) toTarget:self withObject:url];
}

When I make a php to xml with echo directly, like this way,

......
echo "<entry><title>this is the TEST</title><item>TEST</item></entry>";
......

iPhone app does parse this code like XML. But when I make a php to xml with mySQL query (cause I want to make a xml items from DB), like this way,

<?php
echo '<?xml version="1.0" encoding="UTF-8"?>';
$result = mysql_connect("localhost", "my ID", "my Password");
mysql_select_db("my DB");
$q = "select name, price, age, likeit, keyword from Table where category=101";
$result = mysql_query($q);
$num_rows = mysql_num_rows($result);

echo "<entry>\n";
for ($i=1; $i<=$num_rows; $i++) {
    $row = mysql_fetch_assoc($result);
    echo "<item>\n";
    echo "<title>" . $row["name"] . "</title>\n";
    echo "<category>" . $row["price"] . "</category>\n";
    echo "<artist>" . $row["age"] . "</artist>\n";
    echo "<album>" . $row["likeit"] . "</album>\n";
    echo "<releasedate>" . $row["keyword"] . "</releasedate>\n";
    echo "</item>\n";
}
echo "</entry>";
?>

iPhone app doesn't parse this code. It tells me there's no item in XML. What is the most strange to me, is the results on Web Browser are same exactly. When I put the url in browser, the output itself and the source(with viewing source function of browser) are exactly same. This is the source view in web browser.(Plz don't mind some encoding problem)

<?xml version="1.0" encoding="UTF-8"?>
<entry>
<item>
<title>������ ���ĺ� ������</title>
<category>11000</category>
<artist>3</artist>
<album>0</album>
<releasedate>���ĺ� ���߱�</releasedate>
</item>
<item>
<title>���ĺ� ��������</title>
<category>18000</category>
<artist>3</artist>
<album>0</album>
<releasedate>���ĺ� ����</releasedate>
</item>
…..

I've tried hard to make it work but it's too difficult for me. I am a starter in iOS and Web Programming. Please let me know what is the problem and solution. Thank u in advance!:D

(Plz don't mind some encoding problem)
Maybe we don't mind but the xml parser probably does.
You should

  • set the mimetype in the http response header
  • set the charset in the http response header (though it's already in the xml declaration)
  • set mysql's client encondig to utf8 in order to receive the data utf-8 encoded
  • treat all the data from the database with an appropriate escape function
  • or even better use something like XMLWriter

and you should also print error messages as a somewhat valid xml document since you told the client that it will receive xml.

Eg (tested only by php -l ):

 <?php if ( headers_sent() ) { die("can't set mimetype and/or charset after output has been sent to the client"); } ini_set('default_charset', 'utf-8'); ini_set('default_mimetype', 'text/xml'); // ini_set('default_mimetype', 'application/xml'); echo '<?xml version="1.0" encoding="UTF-8"?>'; $mysql = mysql_connect("localhost", "my ID", "my Password"); if ( !$mysql ) { die('<error>database connection failed</error>'); } if ( !mysql_select_db("my DB", $mysql) ) { die('<error>database selection failed</error>'); } if ( !mysql_set_charset('utf8', $mysql) ) { die('<error>setting database selectiocharset failed</error>'); } $q = 'SELECT name, price, age, likeit, keyword FROM Table WHERE category=101'; $result = mysql_query($q, $mysql); if ( !$result ) { die('<error>database query failed</error>'); } echo "<entry>\\n"; while( false!=($row=mysql_fetch_assoc($result)) ) { echo ' <item> <title>', htmlspecialchars($row["name"], 'utf-8'), '</title> <category>', htmlspecialchars($row["price"], 'utf-8'), '</category> <artist>', htmlspecialchars($row["age"], 'utf-8'), '</artist> <album>', htmlspecialchars($row["likeit"], 'utf-8'), '"</album> <releasedate>', htmlspecialchars($row["keyword"], 'utf-8'), '</releasedate> </item>'; } echo "</entry>"; ?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM