【问题标题】:creating xml doc from mysql database using php使用php从mysql数据库创建xml doc
【发布时间】: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>

请帮帮我。

【问题讨论】:

标签: php mysql xml


【解决方案1】:

您不能有多个根元素。您需要在循环外分配一个根元素,并将case 元素附加到它:

<?xml version="1.0"?>
<cases>
    <case>
        <caseno>010301</caseno>
        <petname>ashiq</petname>
        <resname>state</resname>
        <hearing>8012-08-02</hearing>
        <status>P</status>
    </case>
    <case>
        <caseno>010302</caseno>
        <petname>hussain</petname>
        <resname>state</resname>
        <hearing>8012-08-02
        </hearing>
        <status>P</status>
    </case>
</cases>

另外,顺便说一句:

Please, don't use mysql_* functions in new code。它们不再维护,deprecation process 已开始使用。看到red box?改为了解prepared statements,并使用PDOMySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial

【讨论】:

  • 我已将 元素放在循环之外,但未附加。请编辑我的 php 代码。我需要与您编辑过的完全相同的结果>
  • @SajadA:您需要将所有 &lt;case&gt; 元素附加到该 &lt;stage&gt; 根元素,而不是附加到 DOM 对象本身。
  • 感谢@Madara 实际上我做错了。 Inshallah 我将使用 PDO 而不是直接查询。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-23
  • 1970-01-01
  • 2012-01-23
  • 2013-08-30
  • 1970-01-01
  • 2017-05-21
相关资源
最近更新 更多