首頁 >後端開發 >php教程 > 新手请问一个关于PHP 从数据库取得数据后怎么填充到XML文件中

新手请问一个关于PHP 从数据库取得数据后怎么填充到XML文件中

WBOY
WBOY原創
2016-06-13 13:25:451055瀏覽

新手请教一个关于PHP 从数据库取得数据后如何填充到XML文件中
需求:
我已知用户ID,根据用户ID查询该用户的相关信息,后台SQL语句已经写好,现在是在逻辑层中通过节点方式将数据填充到XML中,然后再通过前段JS代码取得数据, 麻烦请各位牛牛们帮我看下我写的这个逻辑层代码有什么问题,

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->// 引用数据库处理文件
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;
#源码
#<?xml version="1.0" encoding="utf-8"?>
#<xml>
#    10>
#    <name>SOM</name>
#    admin1>
#    1002>
#    
#        <name>TOM</name>
#        Lily0>
#    3>
#</xml> <div class="clear">
                 
              
              
        
            </div>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn