Parsing XML with SimpleXML

Looking for some help parsing an XML document with SimpleXML. First, a few points of reference:

Original XML file: http://pastebin.com/Dn1h62bu
Loaded with SimpleXML: http://pastebin.com/BZV6wAMZ

The second pasetbin page shows where I’m at. I’ve got the data into an array, albeit a confusing one. This array has two “sets” of data, one named Carter Club and the other Frontier Vineyards. My goal is to get the values for the [diningAttributes] array out, for Carter Club.

I am stuck with the syntax of how to get that specific data out of this array. Obviously the output of what I’m looking for is an array, but just a subset of this larger one.

It’s also worth noting that while the Carter Club information resides under index 0, I won’t know that. If I wanted to access the information under Frontier Vineyards instead, I would only have the site name and not have the index value.

Thoughts?

I decided to go a different route and used xml_parse_create() and xml_parse_into_struct() to create a ginormous number of arrays. After a few ugly nested loops, I managed to get the data I was after. No need to hurt your head over this one.

Suppose your object is called $obj, then:


function myFunc($obj) {
  foreach($obj->siteProfile as $profile) {
    if($profile->siteName == "Carter Club") {
      return $profile->diningSiteProfile->diningTableAttribute;
    }
  }
}

Building on Adam’s example, you could use XPath to find the site profile rather than loop over all of them.


$profiles = $obj->xpath('siteProfile[siteName = "Carter Club"]');
if (!empty($profiles)) {
    $profile = $profiles[0];
    
    // Do something with the proflie
    echo $profile->siteAddress->addres1;
}

Thanks to both of you!