Q:

Parsing XML with PHP Not Working

Why does $xml->accommodation['id'] work but $xml->information['items'] does not?
Here's my xml:
<information items="192">
<accommodation id="41457" code="7565E59E-77D8-4E24-8F40-85ABBB99CCD0"/>
<accommodation id="41597" code="1C858B57-F634-4FBB-877F-1D8831417A8B"/>
(etc.)

Here's my PHP:
$url = "URL TO XML FILE";
$xml = simplexml_load_file($url);

(I'm VERY new to using xml. In case you couldn't tell!)

A:

To get the id attribute of the first accommodation node, use;
$xml->accommodation['id']

To get the items attribute of the information node use;
$xml['items']

For example;
<?php

$str = '
  <information items="192">
    <accommodation id="30"/>
    <accommodation id="50"/>
    <accommodation id="80"/>
  </information>
';

$xml = simplexml_load_string($str);

$a = $xml->accommodation['id'];
$b = $xml['items'];

echo "$a, $b";    // Output: 30, 192

?>

The -> means children of and can be chained eg; $xml->foo->bar['id'].
Hope that helps.

