2013-03-27 90 views
0

我使用下面的PHP代碼來創建一個XML文件:如何將圖像標籤插入XML文件?

echo '<?xml version="1.0" encoding="ISO-8859-1"?> 
<rss version="2.0"> 
<channel> 
<title>mytitle</title> 
<description>my description</description> 
<link>www.mysite.com</link>'; 

$sql = "SELECT title, description, url, picture, formatted_date 
FROM somewhere" ; 

$result = mysql_query($sql); 
if (!$result) { 
    die('Invalid query: ' . mysql_error()); 
} 

if(mysql_num_rows($result)>0) 
{ 
    while($result_array = mysql_fetch_assoc($result)) 
    { 
    //timestamp date to xml format 
    $pubdate = date('D, d M Y H:i:s O', strtotime($result_array[formatted_date])); 

     echo ' 
     <item> 
      <title>'.$result_array[title].'</title> 
      <description>'.$result_array[description].'</description> 
      <link>'.$result_array[indit_url].'</link> 
      <pubDate>'.$pubdate.' GMT</pubDate> 

      <enclosure url="'.$result_array[picture].'" length="1121990" type="image"/> 
      <image> 
       <url>'.$result_array[picture].'</url> 
       <title>image title</title> 
       <link>'.$result_array[picture].'</link>   
       <width>111</width>        
       <height>33</height>        
       <description>An amazing picture</description> 
    </image>  </item>'; 

創建使用http://validator.w3.org/正確驗證,但該文件當我嘗試使用一些XML解析器,如:http://simplepie.org/的perser無法捕捉圖像。我是否使用正確的標記插入圖像到XML文件?有沒有其他標籤或方法可以嵌入圖像?

+1

如果您插入圖片或文本或其他任何內容,xml無所謂。它只是一種標記語言。它沒有「圖像」。 – 2013-03-27 17:53:34

+0

馬克B說什麼。也許你想看看[XSLT](http://en.wikipedia.org/wiki/XSLT)? – ficuscr 2013-03-27 17:55:12

回答

0

請嘗試轉義圖像標記中的特殊字符。

$result_array[picture]

也使用htmlspecialchars,你可以在這裏展示您的RSS提要的樣本,這樣的答案可以更準確。

0

我建議在XML逃逸結構中的數據 - 作爲周圍這樣的:

<image> 
    <url><![CDATA[ imagedatagoeshere ]]></content></url> 
</image> 

不過,我不熟悉了SimplePie,所以我不知道,妥善理解CDATA。