Oracle result Array to XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rahma
Forum Newbie
Posts: 5
Joined: Mon Oct 24, 2005 2:44 pm

Oracle result Array to XML

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't believe the output you are wanting is valid XML.. :?
rahma
Forum Newbie
Posts: 5
Joined: Mon Oct 24, 2005 2:44 pm

Post by rahma »

You mean, the format of the XML is wrong....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that's what I said..
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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>
rahma
Forum Newbie
Posts: 5
Joined: Mon Oct 24, 2005 2:44 pm

Post 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>
Post Reply