我是一個不熟練的PHP用戶試圖生成一個XML RSS文檔,
使用PHP類RSS_PHP RSS Parserforeach導致數組中,我怎麼能得到元素的值而不是數組?
的foreach
收益30 <items>
這是很好的,因爲這是這是項目的XML源上的數字,但是當試圖讓<title>
結果這是一個Array
我不知道我失蹤或者我需要改變爲從source而不是數組得到<title>
的價值是什麼價值。
任何幫助將不勝感激,謝謝。
<?php
require_once 'rss_php.php';
header('Content-Type: text/xml; charset=utf-8', true); //set document header content type to be XML
\t $xml = new SimpleXMLElement('<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom"></rss>');
\t $xml->addAttribute('version', '2.0');
\t $rss = new rss_php;
$rss->load('http://rss.tvguide.com/breakingnews');
$items = $rss->getItems();
\t
\t $channel = $xml->addChild('channel'); //add channel node
\t $title = $channel->addChild('title','Channel Title Here'); //title of the feed
\t $link = $channel->addChild('link','http://www.zstreambox.com'); //feed site
\t $description = $channel->addChild('description','Channel description here'); //feed description
\t $language = $channel->addChild('language','en-us'); //language
\t $ttl = $channel->addChild('ttl','120'); //time
\t $image = $channel->addChild('image'); //add atom node
\t $url = $image->addChild('url','http://static.tvgcdn.net/www/img/tvguide-feed-logo.png'); //add channel image
\t $title = $image->addChild('title','ZSB: Breaking News'); //add channel title
\t $link = $image->addChild('link','http://www.zstreambox.com'); //add channel link
\t foreach($items as $index => $item) { \t \t
\t \t $item = $channel->addChild('item'); //add item node
\t \t $title = $item->addChild('title', $item['title']); //add title node
}
echo $xml->asXML();
?>
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Channel Title Here</title>
<link>http://www.zstreambox.com</link>
<description>Channel description here</description>
<language>en-us</language>
<ttl>120</ttl>
<image>...</image>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
</channel>
</rss>
該示例通過PHP類網站提供的是:
<?php
require_once 'rss_php.php';
$rss = new rss_php;
$rss->load('http://digg.com/rss/index.xml');
$items = $rss->getItems();
$html = '';
foreach($items as $index => $item) {
$html .= '<p><a href="'.$item['link'].'" title="'.$item['title'].'"><strong>'.$item['title'].'</strong></a><br />
'.$item['description'].'<br />
Submitted by: <a href="'.$item['digg:submitter']['digg:userimage'].'">'.$item['digg:submitter']['digg:username'].'</a> :: '.$item['pubDate'].'<br />
Diggs: '.$item['digg:diggCount'].' :: <em>Category: '.$item['digg:category'].'</em>
</p>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Digg Front Page</title>
</head>
<body>
<?php
echo $html;
?>
</body>
</html>
我只想包括糾正,萬一有人工作的代碼需要它 感謝驚人的人誰共享,並在此真棒網站幫助基礎上
<?php
require_once 'rss_php.php';
header('Content-Type: text/xml; charset=utf-8', true); //set document header content type to be XML
\t $xml = new SimpleXMLElement('<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom"></rss>');
\t $xml->addAttribute('version', '2.0');
\t $rss = new rss_php;
$rss->load('http://rss.tvguide.com/breakingnews');
$items = $rss->getItems();
\t
\t $channel = $xml->addChild('channel'); //add channel node
\t $title = $channel->addChild('title','Channel Title Here'); //title of the feed
\t $link = $channel->addChild('link','http://www.zstreambox.com'); //feed site
\t $description = $channel->addChild('description','Channel description here'); //feed description
\t $language = $channel->addChild('language','en-us'); //language
\t $ttl = $channel->addChild('ttl','120'); //time
\t $image = $channel->addChild('image'); //add atom node
\t $url = $image->addChild('url','http://static.tvgcdn.net/www/img/tvguide-feed-logo.png'); //add channel image
\t $title = $image->addChild('title','ZSB: Breaking News'); //add channel title
\t $link = $image->addChild('link','http://www.zstreambox.com'); //add channel link
\t foreach($items as $index => $item) {
\t $xml_item = $channel->addChild('item'); //add item node
\t $title = $xml_item->addChild('title', ''.$item['title'].'');
\t $description = $xml_item->addChild('description', ''. $item['description'] . '');
\t }
echo $xml->asXML();
?>
那麼,哪裏'title'從何而來? –
在創建它之後不會附加到根元素 – charlietfl
@charlietfl謝謝..很抱歉,請問您,請給我更多關於如何附加到根文檔的信息?謝謝。 – Peter