How to include a xml in a php page

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
avsa
Forum Newbie
Posts: 1
Joined: Mon Jan 28, 2008 7:42 pm

How to include a xml in a php page

Post by avsa »

I am trying to include a google calendar feed on a hmtl/php page. Google exports its calendars on a nice xml page ( http://www.google.com/calendar/feeds/o5 ... blic/basic ) and I want to separate some entities and include on my page to be styled later through css.

Seems very straightforward but this is my first project on php programming and I keep banging my head. I tried domxml_open_mem and tried a xml parser php class but with no success.

Could anyone point me in the right direction?

thanks a lot
thamizhchelvan
Forum Newbie
Posts: 23
Joined: Tue May 08, 2007 2:50 am

Re: How to include a xml in a php page

Post by thamizhchelvan »

I am using magic parser to parse xml data, try here with your url:

http://www.magicparser.com/demo
User avatar
hannnndy
Forum Contributor
Posts: 131
Joined: Sat Jan 12, 2008 2:09 am
Location: Iran>Tehran
Contact:

Re: How to include a xml in a php page

Post by hannnndy »

use this function which gets the xml stream and then returns the array

Code: Select all

       
                 public function xmlToArray($domnode, &$array)
        {
            $array_ptr = &$array; 
            $domnode = $domnode->firstChild; 
            while (!is_null($domnode)) 
            { 
                if (! (trim($domnode->nodeValue) == "") ) 
                { 
                    switch ($domnode->nodeType) 
                    { 
                        case XML_TEXT_NODE: 
                        { 
                            $array_ptr['data'] = $domnode->nodeValue; 
                            break; 
                        } 
                        case XML_ELEMENT_NODE: 
                        { 
                            $array_ptr = &$array[$domnode->nodeName][]; 
                            if ($domnode->hasAttributes() ) 
                            { 
                                $attributes = $domnode->attributes; 
                                if (!is_array ($attributes)) 
                                { 
                                    break; 
                                } 
                                foreach ($attributes as $index => $domobj) 
                                { 
                                    $array_ptr[$index] = $array_ptr[$domobj->name] = $domobj->value; 
                                } 
                            } 
                            break; 
                        } 
                    } 
                    if ( $domnode->hasChildNodes() ) 
                    { 
                        $this->xmlToArray($domnode, $array_ptr); 
                    } 
                } 
                $domnode = $domnode->nextSibling; 
            } 
        }   // xmlToArray
 
then use

Code: Select all

 
$array = array();
$domnode;
print_r(xmlToArray($domnode, &$array));
 
to view the contents
Post Reply