-1

Possible Duplicate:
A simple program to CRUD node and node values of xml file

I tried calling the data in my XML in the below way. But it didn't work for me. Can anybody tell me how can I make it work?

<broad>
    <site>
        <title>My Site Name</title>
        <caption>My Site Caption</caption>
        <hostname>www.mydomain.com</hostname>
    </site>
<broad>

My PHP file is

<?php
$xml = simplexml_load_file('settings.xml');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title><?php echo $xml->title; .'|'. $xml->caption; ?></title>
    </head>

    <body>
    </body>
</html>

Why the above way is not working? is there any other way which is more easier than this?

Community
  • 1
  • 1
Karthik Malla
  • 5,570
  • 12
  • 46
  • 89
  • First at all delete the first ';' because ';' will break echo statment and you want to concat with caption – Kakawait Jan 12 '13 at 11:02
  • You are missing the 'broad' and 'site' I believe. Probably $xml->broad->site->title will work. In any case a var_dump($xml); will reveal the structure – scott Jan 12 '13 at 11:02
  • Yeah @scott - Calling as $xml->site->title; helped me. – Karthik Malla Jan 12 '13 at 11:05

3 Answers3

2

You need to access the site node:

<?php echo (string) $xml->site->title?>

If ever in doubt, use a var_dump:

<?php echo '<pre>'; var_dump($xml); echo '</pre>'; ?>
hohner
  • 11,498
  • 8
  • 49
  • 84
  • It's important to keep in mind that `var_dump`, `print_r`, etc will **not** tell you all that is in a SimpleXML object for anything other than the simplest XML, due to the large amount of "magic" built into the SimpleXML module. I've written [some specific debug functions](https://github.com/imsop/simplexml_debug) for use with more complex XML. – IMSoP Jan 13 '13 at 17:38
0

Use the following:

<?php echo (string)$xml->site->title .'|'. (string)$xml->site->caption; ?>
0

Use SimpleXML debug with var_dump() and print_r()

laxonline
  • 2,657
  • 1
  • 20
  • 37
  • It's important to keep in mind that `var_dump`, `print_r`, etc will **not** tell you all that is in a SimpleXML object for anything other than the simplest XML, due to the large amount of "magic" built into the SimpleXML module. I've written [some specific debug functions](https://github.com/imsop/simplexml_debug) for use with more complex XML. – IMSoP Jan 13 '13 at 17:39