php - What's the best way to process this XML feed? -


i've got xml document gives me addresses. here's excerpt:

<ezi:orderaddresses>         <ezi:orderaddress>             <ezi:addresstype>billing</ezi:addresstype>             <ezi:name>jason fonseca</ezi:name>             <ezi:companyname>cyber</ezi:companyname>             <ezi:address1>8 springstein</ezi:address1>             <ezi:division>nt</ezi:division>             <ezi:postalcode>34245</ezi:postalcode>             <ezi:countrycode>au</ezi:countrycode>             <ezi:email>jason@bigcreative.com.au</ezi:email>             <ezi:phone>89549854</ezi:phone>             <ezi:mobilephone>984590598</ezi:mobilephone>         </ezi:orderaddress>         <ezi:orderaddress>             <ezi:addresstype>shipping</ezi:addresstype>             <ezi:name>jason fonseca</ezi:name>             <ezi:companyname>cyber</ezi:companyname>             <ezi:address1>8 springstein</ezi:address1>             <ezi:division>nt</ezi:division>             <ezi:postalcode>34245</ezi:postalcode>             <ezi:countrycode>au</ezi:countrycode>             <ezi:email>jason@bigcreative.com.au</ezi:email>             <ezi:phone>89549854</ezi:phone>         </ezi:orderaddress>         </ezi:orderaddresses> 

in addition above format of 2 "orderaddress" tags, there can one, example:

      <ezi:orderaddresses>         <ezi:orderaddress>             <ezi:addresstype>billing</ezi:addresstype>             <ezi:name>jason fonseca</ezi:name>             <ezi:companyname>cyber</ezi:companyname>             <ezi:address1>8 springstein</ezi:address1>             <ezi:division>nt</ezi:division>             <ezi:postalcode>34245</ezi:postalcode>             <ezi:countrycode>au</ezi:countrycode>             <ezi:email>jason@bigcreative.com.au</ezi:email>             <ezi:phone>89549854</ezi:phone>             <ezi:mobilephone>984590598</ezi:mobilephone>         </ezi:orderaddress>         </ezi:orderaddresses> 

what find when using simple xml interpret this, in first instance, following:

 [orderaddresses] => simplexmlelement object     (         [orderaddress] => array             (                 [0] => simplexmlelement object                     (                         [addresstype] => billing                         [name] => jason fonseca                         [companyname] => cyber                         [address1] => 8 springstein                         [division] => nt                         [postalcode] => 34245                         [countrycode] => au                         [email] => jason@bigcreative.com.au                         [phone] => 89549854                         [mobilephone] => 984590598                     )                  [1] => simplexmlelement object                     (                         [addresstype] => shipping                         [name] => jason fonseca                         [companyname] => cyber                         [address1] => 8 springstein                         [division] => nt                         [postalcode] => 34245                         [countrycode] => au                         [email] => jason@bigcreative.com.au                         [phone] => 89549854                     )              )      ) 

and in second instance, this:

   [orderaddresses] => simplexmlelement object         (             [orderaddress] => simplexmlelement object                 (                     [addresstype] => billing                     [name] => jason fonseca                     [companyname] => cyber                     [address1] => 8 springstein                     [division] => nt                     [postalcode] => 34245                     [countrycode] => au                     [email] => jason@bigcreative.com.au                     [phone] => 89549854                     [mobilephone] => 984590598                 )          ) 

the keen observer notice if try access orderaddresses->orderaddress have different structure depending on whether there 2 addresses (or more) or 1 address. example 2 numerically index array, example 2 associative object.

to standardise this, i've used code looks :

if(!isset($content['orderlines']['orderline'][0]))     {         $temp = $content['orderlines']['orderline'];         unset($content['orderlines']['orderline']);         $content['orderlines']['orderline'][0] =$temp;     } 

(you can ignore fact i'm using associative arrays here, i've got routine performs conversion i've checked , routine not change result).

my question is, how supposed correctly inteperet data? having slice of code everytime try access orderaddress , order lines messy. isn't there better way?

the thought of editing conversion routine handle has crossed mind. if think way go, let me know, thought myself surely problem must come lot , must have smart, , brief solution it?

thanks

according documentation simplexml should able iterate on , subscript properties if there 1 of them, may 'best' way of achieving want. e.g. :

for($xml->thingtheremaybeoneormoreof $item) {   //handle $item } 

or, if need handle first item

$item=$xml->thingtheremaybeoneormoreof[0]; //handle $item 

also, according documentation these properties not in fact arrays, accessible (subscriptable) , iterable properties, var_dump etc convert them / treat them arrays purposes of dumping.

i haven't tested this, ymmv.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -