I'm trying to get SimpleXML to display the content from an XML file that I've created. It looks like it's reading the file correctly, however it is not displaying any content. Here is my code and the XML file. Thanks! $xml = simplexml_load_file($plugin); $plugin_name = $xml -> plugin -> name; $plugin_version = $xml -> plugin -> version; $plugin_author = $xml -> plugin -> author; $plugin_url = $xml -> plugin -> url; $plugin_file = $xml -> plugin -> file; PHP: Then the XML file. <?xml version="1.0"?> <plugin> <name>Plugin Name</name> <version>1.0</version> <author>Chris</author> <url>http://www.google.com</url> <file>index.php</file> </plugin> Code (markup): After looking at this, is there something I am doing wrong? The variable $plugin is the string "../plugins/blog/plugin.xml". This should work.
Always remember, debug if you do not see any content: $xml = simplexml_load_file($plugin); echo '<pre>'; print_r($xml); echo '</pre>';
In these snippets you don't echo the variables (the code seems fine, it has to be something else). Sometimes you have to use $var = (string) $xml-> ->variable; because the $xml var holds an array and your $var is a pointer to a section of the $xml data, after the $xml variable is destroyed your $plugin_name variables point to nothing, casting it with (string) forces php to copy the array-data into a separate string.
If you familiar with database, then treat xml like a database. $xml = simplexml_load_file($plugin); $plugin_name = $xml->name; $plugin_version = $xml->version; $plugin_author = $xml->author; $plugin_url = $xml->url; $plugin_file = $xml->file; PHP: