Einsatz der XMLWriter-Klasse

10. Februar 2010 Besucher gesamt: 1680

Habe mal wieder nach einer eleganten PHP-Klasse gesucht, mit der ich bequem und leicht eine RSS- oder XML-Datei abbilden kann. Dabei bin auf die XMLWriter-Klasse gestoßen, und habe sie sofort angewandt. Die Klasse ist sehr intuitiv zu verwenden, jedoch wäre ich glücklicher, wenn mehr Anwendungsbeispiele angezeigt worden wären. Ein sehr gutes Anwendungsbeispiel habe ich im PHPBuilder Forum gefunden, und weil es so gut war, poste ich den Code hier weiter:

// THIS IS ABSOLUTELY ESSENTIAL - DO NOT FORGET TO SET THIS
@date_default_timezone_set("GMT"); 

$writer = new XMLWriter();
// Output directly to the user 

$writer->openURI('php://output');
$writer->startDocument('1.0'); 

$writer->setIndent(4); 

// declare it as an rss document
$writer->startElement('rss');
$writer->writeAttribute('version', '2.0');
$writer->writeAttribute('xmlns:atom', 'http://www.w3.org/2005/Atom'); 

$writer->startElement("channel");
//----------------------------------------------------
//$writer->writeElement('ttl', '0');
$writer->writeElement('title', 'Latest Products');
$writer->writeElement('description', 'This is the latest products from our website.');
$writer->writeElement('link', 'http://www.domain.com/link.htm');
$writer->writeElement('pubDate', date("D, d M Y H:i:s e"));
$writer->startElement('image');
$writer->writeElement('title', 'Latest Products');
$writer->writeElement('link', 'http://www.domain.com/link.htm');
$writer->writeElement('url', 'http://www.iab.net/media/image/120x60.gif');
$writer->writeElement('width', '120');
$writer->writeElement('height', '60');
$writer->endElement();
//---------------------------------------------------- 

//----------------------------------------------------
$writer->startElement("item");
$writer->writeElement('title', 'New Product 8');
$writer->writeElement('link', 'http://www.domain.com/link.htm');
$writer->writeElement('description', 'Description 8 Yeah!');
$writer->writeElement('guid', 'http://www.domain.com/link.htm?tiem=1234'); 

$writer->writeElement('pubDate', date("D, d M Y H:i:s e")); 

$writer->startElement('category');
$writer->writeAttribute('domain', 'http://www.domain.com/link.htm');
$writer->text('May 2008');
$writer->endElement(); // Category 

// End Item
$writer->endElement();
//---------------------------------------------------- 

// End channel
$writer->endElement(); 

// End rss
$writer->endElement(); 

$writer->endDocument(); 

$writer->flush();

Danach sieht die XML-Datei so aus:

<?xml version=“1.0″?>
<rss version=“2.0″ xmlns:atom=“http://www.w3.org/2005/Atom“>
<channel>
<title>Latest Products</title>
<description>This is the latest products from our website.</description>
<link>http://www.domain.com/link.htm</link>
<pubDate>Thu, 10 Jul 2008 11:47:14 GMT</pubDate>
<image>
<title>Latest Products</title>
<link>http://www.domain.com/link.htm</link>
<url>http://www.iab.net/media/image/120×60.gif</url>
<width>120</width>
<height>60</height>
</image>
<item>
<title>New Product 8</title>
<link>http://www.domain.com/link.htm</link>
<description>Description 8 Yeah!</description>
<guid>http://www.domain.com/link.htm?tiem=1234</guid>
<pubDate>Thu, 10 Jul 2008 11:47:14 GMT</pubDate>
<category domain=“http://www.domain.com/link.htm“>May 2008</category>
</item>
</channel>
</rss>

¬ geschrieben von gjerokrsteski in PHP Tricks und Tipps  ¬ Erzähl´s weiter Twitter  | Mr. Wong  | Delicious  | Del.icio.us  | Google  | Facebook

«

» 

Hinterlasse einen Kommentar

*Codebeispiele können im CODE-Tag angegeben werden.

Powered by Wordpress • Abonniere den RSS Feed