Chief Technology Officer & Photographer | Irvine, CA

Dealing with XML in PHP5

Recently, I’ve had a few projects come up dealing with XML.  I’m a PHP programmer, and I run PHP5 on my server, so my first choice to attack XML in PHP is by using the SimpleXML object in PHP.  This has got to be one of gods gifts to PHP programmers…all you have to do is give the SimpleXML object a valid XML file/stream and it’ll put it into an object that you can easily reference like this:

$xml->customer[0]->emailaddresses[0]->emailaddress;

Makes life WAY easier by having that, however, I am currently struggling with an XML issue using SimpleXML and the XML data I am working on out of a Highrise account.  The thing is, in PHP, a valid variable can not have a hyphen in it, so if you come across an XML element named “first-name”, you can’t directly reference it using the method above.

What I’m thinking I’m going to have to do here is some PHP magic via old school PHP4 programming.  I wrote a quick and dirty XML parsing app for Brent (@iboughtamac) a few weeks back…I think I may use some of that code to scan through the elements and rewrite the element names w/o the hyphens.

Anyone else experience this issue when using SimpleXML?  Any ideas you can throw my way?