【发布时间】:2012-11-19 10:03:17
【问题描述】:
我想根据数据库记录创建 xml 文档,我已经对这些行进行了编码,但我不知道如何将父节点放入其中:
$dom = new DOMDocument("1.0");
//header("Content-Type: text/plain");
while ($row = mysql_fetch_row($dtrs)) {
//create sub toping..
$root = $dom->createElement("case");
$dom->appendChild($root);
// create child element
$caseno = $dom->createElement("caseno");
$root->appendChild($caseno);
// create text node
$caseno_text = $dom->createTextNode($row[0]);
$caseno->appendChild($caseno_text);
// create child element
$petname = $dom->createElement("petname");
$root->appendChild($petname);
// create text node
$pet_text = $dom->createTextNode($row[1]);
$petname->appendChild($pet_text);
// create child element
$resname = $dom->createElement("resname");
$root->appendChild($resname);
// create text node
$res_text = $dom->createTextNode($row[2]);
$resname->appendChild($res_text);
// create child element
$hearing = $dom->createElement("hearing");
$root->appendChild($hearing);
// create text node
$hear_text = $dom->createTextNode($row[3]);
$hearing->appendChild($hear_text);
// create child element
$status = $dom->createElement("status");
$root->appendChild($status);
// create text node
$status_text = $dom->createTextNode($row[4]);
$status->appendChild($status_text);
}
我得到这样的结果,我无法将节点放入其中
<?xml version="1.0"?> <case> <caseno>010301</caseno> <petname>ashiq</petname> <resname>state</resname> <hearing>8012-08-02</hearing> <status>P</status> </case> <caseno>010302</caseno> <petname>hussain</petname> <resname>state</resname> <hearing>8012-08-02</hearing> <status>P</status> </case>
其实我想要这样的结果;
<?xml version="1.0"?> <stage> <case> <caseno>010301</caseno> <petname>ashiq</petname> <resname>state</resname> <hearing>8012-08-02</hearing> <status>P</status> </case> <caseno>010302</caseno> <petname>hussain</petname> <resname>state</resname> <hearing>8012-08-02</hearing> <status>P</status> </case> </stage>
请帮帮我。
【问题讨论】:
-
010301 ashiq state 8012-08-02 P 010302 hussain state 8012-08-02 P -
Please, don't use
mysql_*functions in new code。它们不再维护,deprecation process 已开始使用。看到red box?改为了解prepared statements,并使用PDO 或MySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial.