XML Parse question

Hi all

Having a scratch you head moment here.

I have an xml feed to read which contains data such as

<?xml version="1.0" encoding="utf-8"?>
  <agentGroup code="5752">
    <agentBranch code="199">
      <property propertyID="1227">

        repeat properties

using the following code I can access data that is a subset of <property>, but I cant seem to access the second part of the property tag “propertyID=1227”

foreach ($properties->agentGroup->agentBranch->property as $entry) { // gets each property

  $kfpropref = $entry->property->propertyID;    // THIS LINE DOESNT WORK
  $address = $entry->address;
  $summary = addslashes($entry->summary);
  $details = addslashes($entry->details);
  $price = $entry->price;
  $state = $entry->sellingState;
  $keywords = addslashes($entry->additionalKeywords);


How can I extract that info


OK, Ive found it.

Bizzarely this works

$kfpropref = $entry->attributes()->propertyID;

Not that I have anything against writing your own parser or anything but I recommend you look into the DOMCrawler that is a component within Symfony 2. The DOMCrawler coupled with the [url=http://symfony.com/doc/current/components/css_selector.html]CSSSelector component make for a very powerful DOM parser. If you need to do a lot of parsing than using a library is the way to go. Especially one as well written as DOMCrawler. Just thought I would throw that out there.

thanks oddz Ill give it a look

To access an attribute, array-style syntax can be used.


See also http://php.net/simplexml.examples-basic