1

URLから画像を抽出するために次のコードを実行しましたが、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>";
}
4

3 に答える 3

3

このような単純なXMLチャンクの場合、PHPのSimpleXMLAPIを使用してXMLチャンクを生成でき<response>ます。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エンコーディングを気にする必要はありません。SimpleXMLAPIがこれを行います。

その場合、出力は同様に簡単です。

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>
于 2013-02-12T11:48:34.973 に答える
1

XML で <img src=...> タグを使用して画像を表示することはできません。それがHTMLタグです。アプリケーションが読み取ってレンダリングできる画像への URL を指定できますが、それはアプリケーション固有 (つまり、ブラウザー、携帯電話、またはデスクトップ アプリケーション) です。これが XML のポイントです。単一のアプリケーション。

于 2013-02-12T11:38:39.907 に答える
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>";
       }
于 2013-02-12T11:46:19.970 に答える