2013-02-12 28 views
1

我已經完成了以下代碼以從URL中提取圖像,但是我收到了XML解析錯誤。如何顯示包含所有XML輸出的圖像?用XML輸出顯示圖像

$error = true; 
$msg = 'profile'; 
header('Content-type: text/xml'); 
echo '<?xml version="1.0" encoding="UTF-8"?>'; 
if($error) 
{ 
    echo "<response>"; 
    echo "<success>S</success>"; 
    echo "<message>".trim($msg)."</message>"; 
    echo "<userid>".trim($userid)."</userid>"; 
    echo "<username>".trim($username)."</username>"; 
    echo "<firstname>".trim($firstname)."</firstname>"; 
    echo "<lastname>".trim($lastname)."</lastname>"; 
    echo "<email>".trim($email)."</email>"; 
    echo "<follower>".trim($follower)."</follower>"; 
    //echo "<photo><img src=photo/".trim($photo) ." height='200' width='200'></photo>"; 
    echo "</response>"; 
} 
+0

避免形成個XML這樣的。改用API/LIB。 – SparKot 2013-02-12 11:34:51

+0

我不認爲你可以添加一個圖像到XML輸出,你仍然可以提供它的URL,並以其他方式呈現。 – Gntem 2013-02-12 11:35:49

+0

XML中的屬性必須包含在''''或''''中。 – SparKot 2013-02-12 11:36:13

回答

1

XML不能使用< img src = ...>標籤來顯示圖像。這是一個HTML標籤。您可以擁有應用程序可以讀取然後呈現的圖像的URL,但這是應用程序特定的(即瀏覽器,移動電話或桌面應用程序) - 這是XML的要點,它是通用的,不受限於單一應用。

0

使用單引號src值,然後關閉img標籤

 $error=true; 
     $msg='profile'; 
     header('Content-type: text/xml'); 
     echo '<?xml version="1.0" encoding="UTF-8"?>'; 
     if($error) 
     { 
       echo "<response>"; 
       echo "<success>S</success>"; 
       echo "<message>".trim($msg)."</message>"; 
       echo "<userid>".trim($userid)."</userid>"; 
       echo "<username>".trim($username)."</username>"; 
       echo "<firstname>".trim($firstname)."</firstname>"; 
       echo "<lastname>".trim($lastname)."</lastname>"; 
       echo "<email>".trim($email)."</email>"; 
       echo "<follower>".trim($follower)."</follower>"; 
       echo "<photo><img src='photo/".trim($photo)."' height='200' width='200'></img></photo>"; 
       echo "</response>"; 
     } 
3

對於這樣一個簡單的XML塊,你可以利用PHP's SimpleXML API生成<response> XML塊。對於沒有屬性,它會像XML文檔本身和直接的要素:

$response   = new SimpleXMLElement('<response/>'); 
$response->success = 5; 
$response->message = trim($msg); 

爲了與IMG孩子和屬性的圖片元素,它是這樣:

$img   = $response->addChild('photo')->addChild('img'); 
$img["src"] = "photo/" . trim($photo); 
$img["height"] = 200; 
$img["width"] = 200; 

你找到PHP當你的谷歌爲「SimpleXML的基本示例」的PHP手冊中類似的例子和更多的信息。你可以看到,你不需要關心這裏的XML編碼,SimpleXML API會爲你做這件事。

的輸出是類似直截了當:

header('Content-type: text/xml'); 
echo $response->asXML(); 

我希望這是有幫助的,典型輸出是:

<?xml version="1.0"?> 
<response><success>5</success><message>profile</message><photo><img src="photo/nice-day.jpg" height="200" width="200"/></photo></response> 

,美化:

<?xml version="1.0"?> 
<response> 
    <success>5</success> 
    <message>profile</message> 
    <photo> 
     <img src="photo/nice-day.jpg" height="200" width="200"/> 
    </photo> 
</response> 
+0

+1很好的解釋。 – SparKot 2013-02-12 11:51:09

+0

感謝您解釋它,但我仍然無法顯示圖像。 – TheLittleNaruto 2013-02-12 12:41:30

+0

那麼,修復圖像的路徑,這可能是您的問題。您需要先找出正確的路徑,然後使用正確的路徑。對此,我無能爲力,抱歉。 – hakre 2013-02-12 13:29:14