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

I've got an XML document that gives me addresses. Here's an 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 to the above format of two "orderaddress" tags, there can also be one, for 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 I find is that when using simple xml to interpret this, in the first instance, I get the 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 the second instance, I get 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 will notice that now if I try to access orderaddresses->orderaddress that this would have a different structure depending on whether there are two addresses (or more) or just one address. Example 2 it is a numerically index array, example two it is a an associative object.

To standardise this, I've used some code that looks like this :

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

(you can ignore the fact that i'm using associative arrays here, i've got a routine that performs a conversion but I've checked and this routine does not change the result).

My question is, how am I supposed to correctly inteperet this data? Having that slice of code everytime I try to access the orderaddress and order lines is really messy. Isn't there a better way?

The thought of editing my conversion routine to handle this has crossed my mind. If you think that is the way to go, then let me know, but I thought to myself surely this problem must come up a lot and someone must have a smart, and brief solution to it?

Thanks

Answers


According to the documentation for simplexml you should always be able to iterate over and subscript properties even if there is only one of them, this may be the 'best' way of achieving what you want. e.g. :

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

or, if you only need to handle the first item

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

Also, according to the documentation these properties are not in fact arrays, but accessible (subscriptable) and iterable properties, just that var_dump etc will convert them to / treat them as arrays for the purposes of dumping.

I haven't actually tested this, ymmv.


Need Your Help

Android AsyncTask

android android-asynctask

Am i fully utilising my AsyncTask? Can someone correct my codes if it is wrong. I just want to make sure my AsyncTask fully works so i wont get any trouble in the future. I wish to use AsyncTask fo...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.