user896692
user896692

Reputation: 2371

PHP and XML - Parsing without result

In my php script, I try to parse a online - xml file.

That´s how I load it into my script:

$xml = simplexml_load_file($filename);

Now, I want to make a test output. I try it with

echo $xml->title;

but there is no result. When I try print_r($xml) I get the right XML file. It looks like that:

<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>testtitle</title>

and so on

Why is there no result?

Upvotes: 0

Views: 59

Answers (2)

Tahir Yasin
Tahir Yasin

Reputation: 11709

//test.xml

<?xml version="1.0"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <title>testtitle</title>
    </channel>
</rss>

//test.php

<?php 
$xml = simplexml_load_file('test.xml');
echo $xml->channel->title;
?>

Upvotes: 0

Peter Steenbergen
Peter Steenbergen

Reputation: 125

It is because of the nesting of the elements.

<rss>
-> $xml
<channel>
-> $xml->channel
<title>
-> $xml->channel->title

So if u use:

<?php
echo  $xml->channel->title;
?>

That will output your title

Upvotes: 3

Related Questions