新手請問一個關於PHP 從資料庫取得資料後怎麼填充到XML檔案中

來源:互聯網
上載者:User
新手請教一個關於PHP 從資料庫取得資料後如何填充到XML檔案中
需求:
我已知使用者ID,根據使用者ID查詢該使用者的相關資訊,後台SQL語句已經寫好,現在是在邏輯層中通過節點方式將資料填充到XML中,然後再通過前段JS代碼取得資料, 麻煩請各位牛牛們幫我看下我寫的這個邏輯層代碼有什麼問題,
PHP code
// 引用資料庫處理檔案require_once "../../DBHandle/DBHandle/DBUser.php";$lefNav = $_REQUEST ["lefNav"];$action = $_REQUEST ["action"];if ($lefNav == "userinfo") {    if (action == "init") {        $userId = $_REQUEST ['user_id'];        $rs = DBUser::getUserInfoByID ( $userId );        if ($rs) {                        // 將查詢出來的使用者資訊結果放置到XML中傳到前段            $doc = new DOMDocument ( "1.0", "utf-8" );            $doc->appendChild ( $doc->createElement ( "xml" ) );                            $userNode = $doc->createElement ( "rs" );                                $userId = $doc->createElement ( "userId" );                $userIdTx = $doc->createTextNode($rs[0]);                $userId->appendChild($userIdTx);                                $name = $doc->createElement ( "name" );                $nameTx = $doc->createTextNode($rs[1]);                $name->appendChild($nameTx);                                $rname = $doc->createElement ( "rname" );                $rnameTx = $doc->createTextNode($rs[2]);                $rname->appendChild($rnameTx);                                $gname = $doc->createElement ( "gname" );                $gnameTx = $doc->createTextNode($rs[3]);                $gname->appendChild($gnameTx);                                $userNode->appendChild($userId);                $userNode->appendChild($name);                $userNode->appendChild($rname);                $userNode->appendChild($gname);                                $doc->documentElement->appendChild($userNode);                echo $doc->saveXML();                    } else {            echo "FAIL";        }    }}   


------解決方案--------------------
PHP與XML的相互轉換封裝函數:xmlparse.php
PHP code
include(dirname(__FILE__)."/xmlparse.php");$listtags = explode(" ","2 name");$arr = array(1,"name"=>"SOM","admin",100,array("name"=>"TOM","Lily"));$contXML = dump_xml_config($arr, "xml", "utf-8");//放入檔案//file_put_contents_safe("/path/test.xml",$contXML,"w");echo $contXML;#源碼###    <0>1#    SOM#    <1>admin#    <2>100#    <3>#        TOM#        <0>Lily#    # 
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.