我想让一个工作的xml文件在php(编码正确)导出数据从在线数据库到iphone
请告诉我它的编码…因为我是php新手…我知道的不多:请帮助我:
我现在使用的代码如下:
<?php
// Èdition du dÈbut du fichier XML
$xml .= '<?xml version='"1.0'" encoding='"UTF-8'"?>';
$sml .= '<channel>';
$xml .= '<title>Infonul</title>';
$xml .= '<link>aaa</link>';
$xml .= '<description>aaa</description>';
// connexion a la base (mettre ‡ jour le mdp)
$connect = mysql_connect('...-12','...','...');
/* selection de la base de donnÈe mysql */
mysql_select_db('...');
// selection des 20 derniËres news
$res=mysql_query("SELECT u.display_name as author,p.post_date as date,p.comment_count as commentCount, p.post_content as content,p.post_title as title FROM wp_posts p, wp_users u WHERE p.post_status = 'publish' and p.post_type = 'post' and p.post_author = u.id ORDER BY p.id DESC LIMIT 0,20");
// extraction des informations et ajout au contenu
while($tab=mysql_fetch_array($res)){
$title=$tab[title];
$author=$tab[author];
$content=$tab[content]; //html stuff
$commentCount=$tab[commentCount];
$date=$tab[date];
$xml .= '<item>';
$xml .= '<title>'.$title.'</title>';
$xml .= '<content><![CDATA['.$content.']]></content>';
$xml .= '<date>'.$date.'</date>';
$xml .= '<author>'.$author.'</author>';
$xml .= '<commentCount>'.$commentCount.'</commentCount>';
$xml .= '</item>';
}
// Èdition de la fin du fichier XML
$xml .= '</channel>';
$xml = utf8_encode($xml);
echo $xml;
// Ècriture dans le fichier
if ($fp = fopen("20news.xml",'w'))
{
fputs($fp,$xml);
fclose($fp);
}
//mysql_close();
?>
但是这段代码有一些错误
向iPhone传递XML内容的最佳方式可能是通过plist。
plist文件(filename.plist)实际上就是一种XML格式,但是以一种特殊的方式。它看起来像这样:
<plist version="1.0">
<array>
<dict>
<key>title</key>
<string>Angry Cat</string>
<key>fileName</key>
<string>cat_angry1.caf</string>
<key>fileArray</key>
<array>
<dict>
<key>fileName</key>
<string>cat_angry1.caf</string>
</dict>
</array>
<key>icon</key>
<string>icon1.png</string>
<key>buttonIndex</key>
<integer>0</integer>
</dict>
</array>
</plist>
所以你需要做的就是在php中输出正确的头文件:
header("Content-type: text/xml");
echo "<?xml version='"1.0'"?>";
并将数据库输出为类似于上述格式的格式。
您可以从那里使用NSURLConnection
下载plist,并使用此处的教程:http://iphoneincubator.com/blog/data-management/reading-and-writing-plists来读取plist格式。:)
代码发布后的更新
你的代码有几个问题。首先在第5行下面是$sml .= '<channel>';
应该是$xml...
。数组应该总是用引号括起来。这是错误的:$tab[commentCount]
应该是$tab["commentCount"]
,以防有任何命名相同的常量。
顶部是$xml .= '...
。虽然这可能有用,但我以前从未遇到过。只要试着在最上面把$xml = ''
放在所有代码行之前。然后在尝试向其附加字符串之前初始化该变量。
希望这些有用。如果没有这些问题,那么请您可以张贴文件的输出。或者php/mysql错误,都可能与此有关