0

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

登录.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 应用程序无法解析此结果。我做错了什么?

4

1 回答 1

1

你试过拿出来htmlentities吗?

正如你发送的是:

&lt;xml version ...

不是

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

如你想象的

于 2012-12-02T17:22:17.593 回答