从PHP生成XML输出

时间:2012-12-02 17:15:55

标签: php mysql xml

我检查了许多示例代码,但仍在努力使我的工作正常。我正在尝试从我的PHP脚本生成XML输出。

的login.php

<?php

    mysql_connect("localhost", "root", "") or die("cannot connect"); 
    mysql_select_db("ConquestOfLancaster") or die("cannot select DB");

    $username = $_REQUEST['username'];
    $password = sha1($_REQUEST['password']);

    $username = mysql_real_escape_string($username);

    $domDoc = new DOMDocument;
    $rootElt = $domDoc->createElement('root');
    $rootNode = $domDoc->appendChild($rootElt);

    $sql = "SELECT user_id, user_name FROM tbl_user WHERE user_name='$username' and user_pass='$password'";
    $result = mysql_query($sql);

    $count=mysql_num_rows($result);

    if($count > 0){

        //echo("welcome");
        $rootAtr = $domDoc->createAttribute('success');
        $rootAtrVal = $domDoc->createTextNode('TRUE');
        $rootAtr->AppendChild($rootAtrVal);
        $rootElt->AppendChild($rootAtr);

        $subElt = $domDoc->createElement('username');
        $subNode = $rootNode->appendChild($subElt);

        $textNode = $domDoc->createTextNode($username);
        $subNode->appendChild($textNode);

        echo htmlentities($domDoc->saveXML());
    } else{

        //echo("not exist");
        $rootAtr = $domDoc->createAttribute('success');
        $rootAtrVal = $domDoc->createTextNode('FALSE');
        $rootAtr->AppendChild($rootAtrVal);
        $rootElt->AppendChild($rootAtr);

        echo htmlentities($domDoc->saveXML());
    }

?>

并且浏览器上的输出是

<?xml version="1.0"?> <root success="TRUE"><username>user1</username></root>

我的主要问题是我与这个PHP交互的iOS应用程序无法解析此结果。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您是否尝试取出htmlentities

你发送的是:

&lt;xml version ...

<?xml version="1.0"?> <root success="TRUE"><username>user1</username></root> 

正如你想的那样