how to parse complex xml in php -


i have xml document , i'm trying parse ( loop through items ) seems no xml parser understands it. i've tried simplexml , few others provide 2 fields (@attributes: locale, country) or unclear arrays. using online viewer can see list of items must issue php parser. need extract each item image, price , location. decode in array highly appreciated ! need loop through 'ad' array , extract information each 'ad'. id, price, title, start-date-time, category, locations, pictures (array). here print-screen: http://i.imgur.com/r9ykuzy.png . doesn't let me post image without 10 reputation.

$xml =  simplexml_load_file('xml'); print_r($xml); 

returns

simplexmlelement object (     [@attributes] => array         (             [locale] => en_gb             [version] => 1.17         )  ) 

i tried class i've found ( xml2array ) no luck ! thinking use regex it's old friend of mine data doesn't seem structured in such way extracted regex.

$array = xml2array::createarray($xml); print_r($array); 

below xml. pasted on pastebin.com/xikbfhv5 .

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <ad:ads xmlns:types="http://www.domainclassifiedsgroup.com/schema/types/v1" xmlns:cat="http://www.domainclassifiedsgroup.com/schema/category/v1" xmlns:loc="http://www.domainclassifiedsgroup.com/schema/location/v1" xmlns:ad="http://www.domainclassifiedsgroup.com/schema/ad/v1" xmlns:feat="http://www.domainclassifiedsgroup.com/schema/feature/v1" xmlns:attr="http://www.domainclassifiedsgroup.com/schema/attribute/v1" xmlns:pic="http://www.domainclassifiedsgroup.com/schema/picture/v1" xmlns:user="http://www.domainclassifiedsgroup.com/schema/user/v1" xmlns:rate="http://www.domainclassifiedsgroup.com/schema/rate/v1" xmlns:reply="http://www.domainclassifiedsgroup.com/schema/reply/v1" xmlns:feed="http://www.domainclassifiedsgroup.com/schema/feed/v1" xmlns:order="http://www.domainclassifiedsgroup.com/schema/order/v1" xmlns:payment="http://www.domainclassifiedsgroup.com/schema/payment/v1" locale="en_gb" version="1.17">   <ad:ad id="1046911987">     <ad:title>wanted apple macbook pro - air - retina - imac call wanted today</ad:title>     <ad:ad-status>       <ad:value>active</ad:value>     </ad:ad-status>     <ad:start-date-time>2014-01-31t17:54:24.000z</ad:start-date-time>     <feat:features-active>       <feat:feature-active display="true" name="ad_gp_top_ad" group="standard"/>     </feat:features-active>     <cat:category id="188">       <cat:id-name>computing-phones-wanted</cat:id-name>       <cat:localized-name>computing &amp; phones</cat:localized-name>     </cat:category>     <loc:locations>       <loc:location id="124">         <loc:id-name>wimbledon</loc:id-name>         <loc:localized-name>wimbledon</loc:localized-name>       </loc:location>       <loc:location id="10000392">         <loc:id-name>uk</loc:id-name>         <loc:localized-name>united kingdom</loc:localized-name>       </loc:location>       <loc:location id="10000393">         <loc:id-name>england</loc:id-name>         <loc:localized-name>england</loc:localized-name>       </loc:location>       <loc:location id="375">         <loc:id-name>south-west-london</loc:id-name>         <loc:localized-name>south west london</loc:localized-name>       </loc:location>       <loc:location id="10000344">         <loc:id-name>london</loc:id-name>         <loc:localized-name>london</loc:localized-name>       </loc:location>     </loc:locations>     <pic:pictures>       <pic:picture>         <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/mze3wdu1ma==/z/i8waamxqvt9s6-li/$_80.jpg"/>         <pic:link rel="preview" href="http://i.domainimg.com/00/s/mze3wdu1ma==/z/i8waamxqvt9s6-li/$_81.jpg"/>         <pic:link rel="big" href="http://i.domainimg.com/00/s/mze3wdu1ma==/z/i8waamxqvt9s6-li/$_79.jpg"/>         <pic:link rel="thumb" href="http://i.domainimg.com/00/s/mze3wdu1ma==/z/i8waamxqvt9s6-li/$_78.jpg"/>         <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/mze3wdu1ma==/z/i8waamxqvt9s6-li/$_77.jpg"/>       </pic:picture>     </pic:pictures>   </ad:ad>   <ad:ad id="1046911555">     <ad:price>       <types:currency-iso-code>         <types:value localized-label="£">gbp</types:value>       </types:currency-iso-code>       <types:amount>140</types:amount>     </ad:price>     <ad:title>hillbilly golf trolley batteries, trolley bag, callaway golf clubs</ad:title>     <ad:ad-status>       <ad:value>active</ad:value>     </ad:ad-status>     <ad:start-date-time>2014-01-31t17:50:21.000z</ad:start-date-time>     <feat:features-active>       <feat:feature-active display="true" name="ad_gp_top_ad" group="standard"/>     </feat:features-active>     <cat:category id="160">       <cat:id-name>golf-equipment</cat:id-name>       <cat:localized-name>golf equipment</cat:localized-name>     </cat:category>     <loc:locations>       <loc:location id="10000373">         <loc:id-name>york</loc:id-name>         <loc:localized-name>york</loc:localized-name>       </loc:location>       <loc:location id="11000029">         <loc:id-name>north-yorkshire</loc:id-name>         <loc:localized-name>north yorkshire</loc:localized-name>       </loc:location>       <loc:location id="10000392">         <loc:id-name>uk</loc:id-name>         <loc:localized-name>united kingdom</loc:localized-name>       </loc:location>       <loc:location id="10000393">         <loc:id-name>england</loc:id-name>         <loc:localized-name>england</loc:localized-name>       </loc:location>     </loc:locations>     <pic:pictures>       <pic:picture>         <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/nzy4wdewmjq=/z/390aaoxygpts695l/$_80.jpg"/>         <pic:link rel="preview" href="http://i.domainimg.com/00/s/nzy4wdewmjq=/z/390aaoxygpts695l/$_81.jpg"/>         <pic:link rel="big" href="http://i.domainimg.com/00/s/nzy4wdewmjq=/z/390aaoxygpts695l/$_79.jpg"/>         <pic:link rel="thumb" href="http://i.domainimg.com/00/s/nzy4wdewmjq=/z/390aaoxygpts695l/$_78.jpg"/>         <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/nzy4wdewmjq=/z/390aaoxygpts695l/$_77.jpg"/>       </pic:picture>       <pic:picture>         <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/mtaynfg3njg=/z/ey8aaoxyurzs695z/$_80.jpg"/>         <pic:link rel="preview" href="http://i.domainimg.com/00/s/mtaynfg3njg=/z/ey8aaoxyurzs695z/$_81.jpg"/>         <pic:link rel="big" href="http://i.domainimg.com/00/s/mtaynfg3njg=/z/ey8aaoxyurzs695z/$_79.jpg"/>         <pic:link rel="thumb" href="http://i.domainimg.com/00/s/mtaynfg3njg=/z/ey8aaoxyurzs695z/$_78.jpg"/>         <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/mtaynfg3njg=/z/ey8aaoxyurzs695z/$_77.jpg"/>       </pic:picture>       <pic:picture>         <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/nzy4wdewmjq=/z/tkgaamxqvers696v/$_80.jpg"/>         <pic:link rel="preview" href="http://i.domainimg.com/00/s/nzy4wdewmjq=/z/tkgaamxqvers696v/$_81.jpg"/>         <pic:link rel="big" href="http://i.domainimg.com/00/s/nzy4wdewmjq=/z/tkgaamxqvers696v/$_79.jpg"/>         <pic:link rel="thumb" href="http://i.domainimg.com/00/s/nzy4wdewmjq=/z/tkgaamxqvers696v/$_78.jpg"/>         <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/nzy4wdewmjq=/z/tkgaamxqvers696v/$_77.jpg"/>       </pic:picture>     </pic:pictures>   </ad:ad>   <ad:ad id="1046912276">     <ad:price>       <types:currency-iso-code>         <types:value localized-label="£">gbp</types:value>       </types:currency-iso-code>       <types:amount>130</types:amount>     </ad:price>     <ad:title>beyonce mrs carter show**top price seats* reduced price!!</ad:title>     <ad:ad-status>       <ad:value>active</ad:value>     </ad:ad-status>     <ad:start-date-time>2014-01-31t17:56:01.000z</ad:start-date-time>     <feat:features-active/>     <cat:category id="84">       <cat:id-name>pop-music-tickets</cat:id-name>       <cat:localized-name>pop</cat:localized-name>     </cat:category>     <loc:locations>       <loc:location id="22010">         <loc:id-name>birmingham-city-centre</loc:id-name>         <loc:localized-name>birmingham city centre</loc:localized-name>       </loc:location>       <loc:location id="10000392">         <loc:id-name>uk</loc:id-name>         <loc:localized-name>united kingdom</loc:localized-name>       </loc:location>       <loc:location id="10000393">         <loc:id-name>england</loc:id-name>         <loc:localized-name>england</loc:localized-name>       </loc:location>       <loc:location id="11000043">         <loc:id-name>west-midlands</loc:id-name>         <loc:localized-name>west midlands</loc:localized-name>       </loc:location>       <loc:location id="10000347">         <loc:id-name>birmingham</loc:id-name>         <loc:localized-name>birmingham</loc:localized-name>       </loc:location>     </loc:locations>     <pic:pictures>       <pic:picture>         <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/odawwdq1mq==/z/oaiaamxqvt9s6-ow/$_80.jpg"/>         <pic:link rel="preview" href="http://i.domainimg.com/00/s/odawwdq1mq==/z/oaiaamxqvt9s6-ow/$_81.jpg"/>         <pic:link rel="big" href="http://i.domainimg.com/00/s/odawwdq1mq==/z/oaiaamxqvt9s6-ow/$_79.jpg"/>         <pic:link rel="thumb" href="http://i.domainimg.com/00/s/odawwdq1mq==/z/oaiaamxqvt9s6-ow/$_78.jpg"/>         <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/odawwdq1mq==/z/oaiaamxqvt9s6-ow/$_77.jpg"/>       </pic:picture>     </pic:pictures>   </ad:ad>   <ad:ad id="1046912270">     <ad:price>       <types:currency-iso-code>         <types:value localized-label="£">gbp</types:value>       </types:currency-iso-code>       <types:amount>0</types:amount>     </ad:price>     <ad:title>brand new bedrooms furniture - best quality &amp; exclusive offers</ad:title>     <ad:ad-status>       <ad:value>active</ad:value>     </ad:ad-status>     <ad:start-date-time>2014-01-31t17:56:01.000z</ad:start-date-time>     <feat:features-active/>     <cat:category id="681">       <cat:id-name>other-bedroom-furniture</cat:id-name>       <cat:localized-name>other bedroom furniture &amp; accs</cat:localized-name>     </cat:category>     <loc:locations>       <loc:location id="10000344">         <loc:id-name>london</loc:id-name>         <loc:localized-name>london</loc:localized-name>       </loc:location>       <loc:location id="10000392">         <loc:id-name>uk</loc:id-name>         <loc:localized-name>united kingdom</loc:localized-name>       </loc:location>       <loc:location id="10000393">         <loc:id-name>england</loc:id-name>         <loc:localized-name>england</loc:localized-name>       </loc:location>     </loc:locations>     <pic:pictures>       <pic:picture>         <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/nduwwdywma==/z/s84aaoxy63fs6-no/$_80.jpg"/>         <pic:link rel="preview" href="http://i.domainimg.com/00/s/nduwwdywma==/z/s84aaoxy63fs6-no/$_81.jpg"/>         <pic:link rel="big" href="http://i.domainimg.com/00/s/nduwwdywma==/z/s84aaoxy63fs6-no/$_79.jpg"/>         <pic:link rel="thumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/s84aaoxy63fs6-no/$_78.jpg"/>         <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/s84aaoxy63fs6-no/$_77.jpg"/>       </pic:picture>       <pic:picture>         <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/nduwwdywma==/z/ehmaaoxyrhbs6-nk/$_80.jpg"/>         <pic:link rel="preview" href="http://i.domainimg.com/00/s/nduwwdywma==/z/ehmaaoxyrhbs6-nk/$_81.jpg"/>         <pic:link rel="big" href="http://i.domainimg.com/00/s/nduwwdywma==/z/ehmaaoxyrhbs6-nk/$_79.jpg"/>         <pic:link rel="thumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/ehmaaoxyrhbs6-nk/$_78.jpg"/>         <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/ehmaaoxyrhbs6-nk/$_77.jpg"/>       </pic:picture>       <pic:picture>         <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/nduwwdywma==/z/vaiaaoxyomds6-nv/$_80.jpg"/>         <pic:link rel="preview" href="http://i.domainimg.com/00/s/nduwwdywma==/z/vaiaaoxyomds6-nv/$_81.jpg"/>         <pic:link rel="big" href="http://i.domainimg.com/00/s/nduwwdywma==/z/vaiaaoxyomds6-nv/$_79.jpg"/>         <pic:link rel="thumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/vaiaaoxyomds6-nv/$_78.jpg"/>         <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/vaiaaoxyomds6-nv/$_77.jpg"/>       </pic:picture>       <pic:picture>         <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/nduwwdywma==/z/kdyaaoxyvabs6-na/$_80.jpg"/>         <pic:link rel="preview" href="http://i.domainimg.com/00/s/nduwwdywma==/z/kdyaaoxyvabs6-na/$_81.jpg"/>         <pic:link rel="big" href="http://i.domainimg.com/00/s/nduwwdywma==/z/kdyaaoxyvabs6-na/$_79.jpg"/>         <pic:link rel="thumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/kdyaaoxyvabs6-na/$_78.jpg"/>         <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/kdyaaoxyvabs6-na/$_77.jpg"/>       </pic:picture>       <pic:picture>         <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/nduwwdywma==/z/mewaaoxyolhs6-nh/$_80.jpg"/>         <pic:link rel="preview" href="http://i.domainimg.com/00/s/nduwwdywma==/z/mewaaoxyolhs6-nh/$_81.jpg"/>         <pic:link rel="big" href="http://i.domainimg.com/00/s/nduwwdywma==/z/mewaaoxyolhs6-nh/$_79.jpg"/>         <pic:link rel="thumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/mewaaoxyolhs6-nh/$_78.jpg"/>         <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/mewaaoxyolhs6-nh/$_77.jpg"/>       </pic:picture>       <pic:picture>         <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/nduwwdywma==/z/va0aaoxycmbs6-ny/$_80.jpg"/>         <pic:link rel="preview" href="http://i.domainimg.com/00/s/nduwwdywma==/z/va0aaoxycmbs6-ny/$_81.jpg"/>         <pic:link rel="big" href="http://i.domainimg.com/00/s/nduwwdywma==/z/va0aaoxycmbs6-ny/$_79.jpg"/>         <pic:link rel="thumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/va0aaoxycmbs6-ny/$_78.jpg"/>         <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/va0aaoxycmbs6-ny/$_77.jpg"/>       </pic:picture>       <pic:picture>         <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/nduwwdywma==/z/myoaaoxyolhs6-n~/$_80.jpg"/>         <pic:link rel="preview" href="http://i.domainimg.com/00/s/nduwwdywma==/z/myoaaoxyolhs6-n~/$_81.jpg"/>         <pic:link rel="big" href="http://i.domainimg.com/00/s/nduwwdywma==/z/myoaaoxyolhs6-n~/$_79.jpg"/>         <pic:link rel="thumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/myoaaoxyolhs6-n~/$_78.jpg"/>         <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/myoaaoxyolhs6-n~/$_77.jpg"/>       </pic:picture>       <pic:picture>         <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/nduwwdywma==/z/cloaaoxyvlns6-oe/$_80.jpg"/>         <pic:link rel="preview" href="http://i.domainimg.com/00/s/nduwwdywma==/z/cloaaoxyvlns6-oe/$_81.jpg"/>         <pic:link rel="big" href="http://i.domainimg.com/00/s/nduwwdywma==/z/cloaaoxyvlns6-oe/$_79.jpg"/>         <pic:link rel="thumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/cloaaoxyvlns6-oe/$_78.jpg"/>         <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/nduwwdywma==/z/cloaaoxyvlns6-oe/$_77.jpg"/>       </pic:picture>       <pic:picture>         <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/mjgzwdqwma==/z/v6eaaoxycmbs6-oj/$_80.jpg"/>         <pic:link rel="preview" href="http://i.domainimg.com/00/s/mjgzwdqwma==/z/v6eaaoxycmbs6-oj/$_81.jpg"/>         <pic:link rel="big" href="http://i.domainimg.com/00/s/mjgzwdqwma==/z/v6eaaoxycmbs6-oj/$_79.jpg"/>         <pic:link rel="thumb" href="http://i.domainimg.com/00/s/mjgzwdqwma==/z/v6eaaoxycmbs6-oj/$_78.jpg"/>         <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/mjgzwdqwma==/z/v6eaaoxycmbs6-oj/$_77.jpg"/>       </pic:picture>     </pic:pictures>   </ad:ad>   <types:paging>     <types:numfound>960522</types:numfound>     <types:link rel="self" href="https://device-api.domainnone.com/api/ads?locationid=10000392&amp;categoryid=2549&amp;page=0&amp;size=2&amp;_ver=1.17"/>     <types:link rel="next" href="https://device-api.domainnone.com/api/ads?locationid=10000392&amp;categoryid=2549&amp;page=1&amp;size=2&amp;_ver=1.17"/>   </types:paging>   <ad:ads-search-options>     <ad:locationid>10000392</ad:locationid>     <ad:categoryid>2549</ad:categoryid>     <ad:sorttype>       <types:value>date_descending</types:value>     </ad:sorttype>   </ad:ads-search-options>   <ad:ads-search-histograms/> </ad:ads> 

if me make famous !

a generic transformation not work. can use xpath fetch data xml , build own data structures (arrays, objects, ...), sql data database.

i don't use simplexml directly dom (simplexml wrapper dom). first need load xml dom document, create xpath object register namespaces data in. can define own aliases independent xml source, in examples use same.

$dom = new domdocument(); //$dom->load($xmlfile); $dom->loadxml($xmlstring); $xpath = new domxpath($dom); $xpath->registernamespace('ad', 'http://www.ebayclassifiedsgroup.com/schema/ad/v1'); $xpath->registernamespace('cat', 'http://www.ebayclassifiedsgroup.com/schema/category/v1'); 

now possible use domxpath::evaluate() fetch data xml. depending on expression, result list of nodes or scalar. expression //element-name return list, count(//element-name) number. second argument of evaluate() context, third should false if have namespaces.

$result = array(); $ads = $xpath->evaluate('/ad:ads/ad:ad', null, false); foreach ($ads $ad) {   $categories = [];   foreach ($xpath->evaluate('cat:category', $ad, false) $category) {     $categories[$category->getattribute('id')] = [       'name' => $xpath->evaluate('string(cat:id-name)', $category, false),       'title' => $xpath->evaluate('string(cat:localized-name)', $category, false)     ];   }   $result[] = [     'id' => $ad->getattribute('id'),     'title' => $xpath->evaluate('string(ad:title)', $ad, false),     'categories' => $categories   ]; } 

simplexmls xpath() method works same, 1 big difference - can return lists, no scalars.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -