0

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!)

hakre
  • 193,403
  • 52
  • 435
  • 836
MarkHFLA
  • 107
  • 1
  • 10
  • possible duplicate of [PHP SimpleXML + Get Attribute](http://stackoverflow.com/questions/10537657/php-simplexml-get-attribute) - You find this explained in depth as well in the [SimpleXML Basic Usage examples](http://php.net/simplexml.examples-basic.php) - next to many other things. So before you try for too long next time, a look in there might give you some hints if not the solution already. – hakre May 02 '13 at 12:55

1 Answers1

0

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.

Nigel Alderton
  • 2,265
  • 2
  • 24
  • 55