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

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

WBOY
WBOY原创
2016-06-13 10:30:58992浏览

新手请教一个关于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>#    <0>1</0>#    <name>SOM</name>#    <1>admin</1>#    <2>100</2>#    <3>#        <name>TOM</name>#        <0>Lily</0>#    </3>#</xml><div class="clear"></div>
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn