2013-02-12 29 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