-1

I am having a syntax issue. The following code prints all of the names from an XML file.

$url = 'http://my-xml-file';
$xml = simplexml_load_file($url);

foreach($xml->PERSON as $PERSON) {
  echo '<div>' . $PERSON->NAME . '</div>';
}

The XML file has now changed and only has 1 person with 1 name in it. How can I modify the code above to echo a single name?

Thanks

UPDATE I have simplified it a bit but the basic XML structure looks like this:

<PERSON>
<NAME>Tom</NAME>
</PERSON>
Jason
  • 15,017
  • 23
  • 85
  • 116
Evanss
  • 23,390
  • 94
  • 282
  • 505

1 Answers1

0
foreach($xml->NAME as $name) {
  echo '<div>' . $name . '</div>';
}

or just

echo '<div>' . $xml->NAME . '</div>';

if there is only 1 entry !

Example : http://codepad.org/g6WUggDq

This is using the XML you supplied .... There should be an outer element surrounding your XML like <document> and a valid header <?xml version='1.0'?>

Manse
  • 37,765
  • 10
  • 83
  • 108