background image

                

<?php

//This query does a search for any Web pages relevant to "XML Query"

$query = 

"http://api.search.yahoo.com/WebSearchService/V1/webSearch?".

 "query=%5C%22XML%20Query%5C%22&app id=YahooDemo" ;

//Create the DOM Document object from the XML returned by the query

$xml = file_get_contents($query);

$dom = new DOMDocument;

$dom = DOMDocument::loadXML($xml);

function xml_to_result($dom) {

 / / Th i s   func t ion  takes  the  XML  document  and  maps  i t   to  a

 / / PHP  ob jec t   so  tha t   you  can  manipu la te  i t   l a te r .

 / / F i r s t ,   re t r i eve  the  roo t   e lement  fo r   the  document

 $root  =  $dom->f i r s tCh i ld ;

 / /Next ,   l oop  th rough  each  o f   i t s   at t r ibu tes

 fo reach($ root - >at t r ibu tes  as  $at t r )   {

 $res [$at t r - >name]  =  $at t r - >va lue ;

 }

 / /Now,  l oop  th rough  each  o f   the  ch i ld ren  o f   the  roo t   e lement

 / / and  t rea t   each  appropr ia te l y.

 / /S ta r t   wi th  the  f i r s t   ch i ld   node .   (The  counte r ,   i ,   i s   fo r

 / / t rack ing  resu l t s .