Novice ask a question about how to populate the XML file after PHP gets the data from the database
Demand:
I know the user ID, according to the user ID query the relevant information of the user, the background SQL statement has been written well, is now in the logical layer through the node way to populate the data into XML, and then through the previous JS code to obtain data, please help me to see what I wrote this logic layer code what is the problem,
PHP Code
Reference database Processing file 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) {//The result of the queried user information is placed in the XML to the previous paragraph $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"; } }}
------Solution--------------------
PHP-to-XML conversion encapsulation function: 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");//Put File//file_put_contents_safe ("/path/test.xml", $ Contxml, "w"); echo $contXML; #源码 # # # # #
<0>1
#
SOM
# <1>admin #
<2>100
# <3>#
TOM #
<0>lily
# #