Page 1 of 1

Oracle result Array to XML

Posted: Mon Oct 31, 2005 1:43 pm
by rahma
Hi All

I am selecting the data from the oracle database into an array and I want this array to be converted into XML.

For example, I am getting the user data from users table "select userName, userPassword from users", the result will be stored in the $results array. I want this array to be converted as follows
<users
<record username="xyz" userPassword="abc123"/>
<record username="abc" userPassword="ytr123"/>
<record username="mno" userPassword="mno123"/>
/>

I tried with following code, I am not getting as above

Code: Select all

$doc = new DomDocument("1.0");
        $root = $doc->createElement("user");
        $root = $doc->appendChild($root);


        foreach($results as $key => $value){
            $body = $doc->createElement("record");
            $body = $root->appendChild($body);
            if (!is_array($value)) {
              $body->setAttribute($key, $value);
            }
            else {
                foreach($value as $data1){

                  $body->setAttribute($key, $data1);
                }
            }
        }
        print $doc->saveXML();
Kindly help me to solve this problem.

Thanks in advance

Regards
Raja

Burrito: Please use

Code: Select all

tags when [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting PHP Code In The Forums[/url][/size]

Posted: Mon Oct 31, 2005 2:10 pm
by feyd
I don't believe the output you are wanting is valid XML.. :?

Posted: Mon Oct 31, 2005 2:16 pm
by rahma
You mean, the format of the XML is wrong....

Posted: Mon Oct 31, 2005 2:21 pm
by feyd
that's what I said..

Posted: Mon Oct 31, 2005 2:42 pm
by Chris Corbyn
You've got tags inside tags. That's not allowed.

Code: Select all

<users>
<record username="xyz" userPassword="abc123"/>
<record username="abc" userPassword="ytr123"/>
<record username="mno" userPassword="mno123"/>
</users>

Posted: Mon Oct 31, 2005 11:42 pm
by rahma
ok, now I understand, thanks for replies. How to get the above mentioned XML format. ie

<users>
<record username="xyz" userPassword="abc123"/>
<record username="abc" userPassword="ytr123"/>
<record username="mno" userPassword="mno123"/>
</users>