0

i want to create xml doc based on database record i have coded these line but i don't know how to put parent node in it:

$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);
}

i get the Result like this where i am not able to put node in it

<?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>

actually i want my result like this;

<?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>

please help me out.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • 010301 ashiq state 8012-08-02 P 010302 hussain state 8012-08-02 P – Sajad A Wani Dec 01 '12 at 08:43
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained and the [deprecation process](http://j.mp/Rj2iVR) has begun on it. See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Madara's Ghost Dec 01 '12 at 08:51

1 Answers1

0

You can't have multiple root elements. You need to assign one root element outside the loop, and append you case elements to it:

<?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>

Also, as an aside:

Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308