【问题标题】:Yii extension XML Parsing Error: no element foundYii 扩展 XML 解析错误:找不到元素
【发布时间】:2015-04-04 23:49:35
【问题描述】:

我正在使用这个 RSS extension 并按照文档中的示例进行操作。

在我看来

 Yii::import('ext.feed.*');
// RSS 2.0 is the default type
$feed = new EFeed();

$feed->title= 'Testing RSS 2.0 EFeed class';
$feed->description = 'This is test of creating a RSS 2.0 Feed';

$feed->setImage('Testing RSS 2.0 EFeed class','http://www.ramirezcobos.com/rss',
'http://www.yiiframework.com/forum/uploads/profile/photo-7106.jpg');

$feed->addChannelTag('language', 'en-us');
$feed->addChannelTag('pubDate', date(DATE_RSS, time()));
$feed->addChannelTag('link', 'http://www.ramirezcobos.com/rss' );

// * self reference
$feed->addChannelTag('atom:link','http://www.ramirezcobos.com/rss/');

$item = $feed->createNewItem();

$item->title = "first Feed";
$item->link = "http://www.yahoo.com";
$item->date = time();
$item->description = 'This is test of adding CDATA Encoded description <b>EFeed Extension</b>';
// this is just a test!!
$item->setEncloser('http://www.tester.com', '1283629', 'audio/mpeg');

$item->addTag('author', 'thisisnot@myemail.com (Antonio Ramirez)');
$item->addTag('guid', 'http://www.ramirezcobos.com/',array('isPermaLink'=>'true'));

$feed->addItem($item);

$feed->generateFeed();
Yii::app()->end();

在我的控制器中

public function actionFeed()
{
    $this->renderPartial('feed');
}

在 Firefox 中我收到此错误

XML Parsing Error: XML or text declaration not at start of entity
Location: http://localhost/dev/frontend/www/abc/bla/feed/
Line Number 1, Column 2: <?xml version="1.0" encoding="UTF-8"?>
-^

在 chrome 中

This page contains the following errors:

error on line 1 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.

扩展中有这个功能

public function generateFeed(){
        header("Content-type: text/xml");
        $this->renderHead();
        $this->renderChannels();
        $this->renderItems();
        $this->renderBottom();
    }

当我注释掉header("Content-type: text/xml"); 时没有错误,但页面显示为 html 并且全部在 1 行中。但是当您查看源代码时,标签都已到位。

第一两行是这样的

 <?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">

知道我做错了什么吗?

【问题讨论】:

    标签: php xml yii rss yii-extensions


    【解决方案1】:

    改变

    &lt;xml version="1.0" encoding="utf-8"&gt;

    &lt;?xml version="1.0" encoding="UTF-8"?&gt;

    查看标签后的问号(?)

    更新:

    你的 xml 应该是一个字符串或像

    这样的单引号内
    <?php header("Content-type: application/xml"); 
    echo  
    '<?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0">
    <channel>
    <title>RSS2 Feed</title>
    <link>http://www.url.com/</link>
    </channel>
    </rss>';
    ?>
    

    【讨论】:

    • 现在我得到了这个Parse error: syntax error, unexpected 'version' (T_STRING) in /Applications/XAMPP/xamppfiles/htdocs/dev/frontend/modules/abc/views/bla/feed.php on line 2,其中&lt;?xml version="1.0" encoding="UTF-8"?&gt; 是第2 行。第1 行是&lt;?php header("Content-type: application/xml"); ?&gt;
    • 现在我收到了XML or text declaration not at start of entity
    猜你喜欢
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    相关资源
    最近更新 更多