Basic simpleXML working example?

SimpleXML 101

  1. First of all, always name your PHP variables after the node they represent.

    // the root node is <programme/>
    $programme = simplexml_load_file("local.xml");
    
  2. Access to children (nodes) as if they were object properties.

    echo $programme->title;
    
  3. If there are multiple children using the same name, you can specify their 0-based position

    // first <title/> child
    echo $programme->title[0];
    
    // create or change the value of the second <title/> child
    $programme->title[1] = 'Second title';
    
  4. Access to attributes as if they were array keys

    // <mynode attr="attribute value" />
    echo $mynode['attr'];
    
  5. XPath always returns an array.


Back to your case, the best way to access that <title /> node would be

$programme = simplexml_load_file("local.xml");
echo "Title: " . $programme->title;

First of all, simplexml xpath method always returns an array of matches. Even if there is only 1 match (or even 0, in which case result is an empty array). This is why you get "Array" in the output.

Secondly, if you want just the title, then you need to change your xpath query:

$result = $xml->xpath("//programme/title");
echo "Title: " . $result[0] . "</br>";

You should probably change the xpath to //programme/title and then echo $result[0] or leave the xpath as it is and echo $result[0]->title. Remember var_dump will always help you.