【问题标题】:Make a working xml file in php (encoding properly) to export data from online database to iphone在 php 中制作一个工作 xml 文件(正确编码)以将数据从在线数据库导出到 iphone
【发布时间】:2011-01-27 03:45:57
【问题描述】:

我正在尝试制作一个 xml 文件以将我的数据从我的数据库导出到我的 iphone。每次我创建一个新帖子时,我都需要执行一个 php 文件来创建一个包含最新帖子的 xml 文件;)

到目前为止还好吗? :D

这是当前的 php 代码……但我的 nsxmlparser 给了我一个错误代码(33 - 字符串未启动)。我不知道我必须使用什么样的 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();

?>

当我在浏览器中打开 20news.xml 时,我注意到了一些事情:

  • 我得到的是正方形而不是单引号 ...
  • 我看不到 清晰可见...为什么?!?

感谢您的任何意见;)

哥蒂。

【问题讨论】:

  • 你能展示一个实际生成的 XML 文件的例子吗?

标签: php iphone xml encoding nsxmlparser


【解决方案1】:

也许您应该使用 XML 库,例如php 的DOM API,而不是字符串连接。省去很多麻烦。

此外,XML 文件需要一个包含所有其他元素的单个根元素才能形成良好的格式(请参阅Wikipedia)。

【讨论】:

  • 感谢您的意见,您还没有解决我的问题,但您给了我一个很好的建议,使用 DOMXML 更清晰;)
【解决方案2】:

这是我的脚本生成的示例代码;)

我从浏览器的源代码中复制粘贴了代码;)

  <?xml version=\"1.0\" encoding=\"UTF-8\"?>
<title>Infonul</title>
<link>aaa</link>
<description>aaa</description>
<item>
<title>Bing évolue et s'anime</title>
<content>
<![CDATA[<p style="text-align: center;"><a href="kkk"><img class="aligncenter size-medium wp-image-548" title="bing" src="http://www.infonul.com/wp-content/uploads/2010/03/bing-300x213.png" alt="" width="300" height="213" /></a></p>
    Vous êtes de plus en plus à l'utiliser, ce moteur de recherche lancé par Microsoft évolue dans sa version française pour ressembler un peu plus à la version US.<!--more--> Comment ? A travers les images de fond qui avaient déjà pour habitude de changer chaque jour vous pourrez maintenant "interagir" avec l'image sur certains points qui pointent sur un site en rapport direct avec l'illustration. Une façon un peu original de présenter le moteur de recherche.]]>
</content>
<date>2010-03-03 23:23:15</date>
<author>kkk kk</author>
<commentCount>0</commentCount>
</item>

<item>
<title>Du quadruple play chez Orange ?</title>
<content>...</content><date>2010-02-28 20:32:13</date><author>...</author><commentCount>0</commentCount>
</item>
</channel>

【讨论】:

  • 好的,解决方法在这里,我只需要添加这段代码:$dom->createCDATASection(utf8_encode($tab['content']))
【解决方案3】:
<?xml version="1.0" encoding="UTF-8"?>
<news>
  <item>
    <author>...</author>
    <title>Bing �volue et s'anime</title>
    <content><![CDATA[<p style="text-align: center;"><a href="..."><img class="aligncenter size-medium wp-image-548" title="bing" src="http://www.infonul.com/wp-content/uploads/2010/03/bing-300x213.png" alt="" width="300" height="213" /></a></p>
Vous �tes de plus en plus � l'utiliser, ce moteur de recherche lanc� par Microsoft �volue dans sa version fran�aise pour ressembler un peu plus � la version US.<!--more-->�Comment ? A travers les images de fond qui avaient d�j� pour habitude de changer chaque jour vous pourrez maintenant "interagir" avec l'image sur certains points�qui pointent sur�un site�en rapport direct avec l'illustration. Une fa�on un peu original de pr�senter le moteur de recherche.]]></content>
  </item>
  <item>
    <author>...</author>

    <title>Du quadruple play chez Orange ?</title>
    <content><![CDATA[<p style="text-align: center;"><a href="http://www.infonul.com/wp-content/uploads/2010/03/logo_orange_print1.gif"><img class="aligncenter size-full wp-image-543" title="orange logo" src="http://www.infonul.com/wp-content/uploads/2010/03/logo_orange_print1.gif" alt="" width="207" height="207" /></a></p>
�

Vous en r�viez ? C'est pour bient�t ! A l'annonce de ses r�sultats pour 2009, le groupe en a profit� pour laisser passer quelques messages � ce sujet.<!--more-->�En effet, Free s'appr�te � d�barquer sur le march� mobile et - il faut le dire - Orange ne propose�RIEN d'avantageux si vous avez votre box et votre mobile chez eux contrairement � Bouygues qui propose ses formules IDEO � des tarifs comp�titifs !Pour quand ? "D�ici � l'�t�"... Affaire � suivre.]]></content>
  </item>
</news>

这就是我在 php5 中使用 DOMXML 所得到的...我的 php 脚本在下面 ;)

<?php

//phpinfo();

$dom = new DOMdocument('1.0', 'UTF-8');
$dom->formatOutput = true;

$r = $dom->createElement('news');
$dom->appendChild( $r );

// 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)){ 

  $b = $dom->createElement( "item" );

  $author = $dom->createElement( "author" );
  $author->appendChild(
  $dom->createTextNode( $tab['author'] )
  );
  $b->appendChild( $author );

  $title = $dom->createElement( "title" );
  $title->appendChild(
  $dom->createTextNode( $tab['title'] )
  );
  $b->appendChild( $title );

  $content = $dom->createElement( "content" );
  $content->appendChild(
  $dom->createCDATASection($tab['content'])
  );
  $b->appendChild( $content );

  $r->appendChild( $b );

  }

  echo $dom->saveXML();
  ?>

【讨论】:

    【解决方案4】:

    如果您的 php 代码与您自己的文件中显示的完全一样,您可以尝试更改行阅读

    $sml .= '<channel>';
    

    $xml .= '<channel>';
    

    您似乎错过了开始标记,该标记被错误地分配给了不同的变量。

    克劳斯

    【讨论】:

    • 谢谢,我自己找到了解决方案……我需要在我的 php 脚本中添加特定的代码行来正确编码;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 2016-05-19
    相关资源
    最近更新 更多