SimpleXML not displaying content

Discussion in 'PHP' started by fireflyproject, Sep 26, 2008.

  1. #1
    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.
     
    fireflyproject, Sep 26, 2008 IP
  2. Resco

    Resco Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #2
    Always remember, debug if you do not see any content:


    $xml = simplexml_load_file($plugin);
    echo '<pre>';
    print_r($xml);
    echo '</pre>';
     
    Resco, Sep 27, 2008 IP
  3. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    juust, Sep 27, 2008 IP
  4. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    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:
     
    php-lover, Sep 27, 2008 IP