教您如何用 php 編寫XML的語法

PHP 教您如何用 php 編寫XML的語法

<?php
$books = array();
$books [] = array(
'title' => 'PHP Hacks',
'author' => 'Jack Herrington',
'publisher' => "O'Reilly"
);
$books [] = array(
'title' => 'Podcasting Hacks',
'author' => 'Jack Herrington',
'publisher' => "O'Reilly"
);

$xmlDATA = "<books>\n";
 
foreach( $books as $book )
{
$title = htmlentities( $book['title'], ENT_QUOTES );
$author = htmlentities( $book['author'], ENT_QUOTES );
$publisher = htmlentities( $book['publisher'], ENT_QUOTES );

$xmlDATA .= "<book>\n";
$xmlDATA .= "<title>$title</title>\n";
$xmlDATA .= "<author>$author</author>\n";
$xmlDATA .= "<publisher>$publisher</publisher>\n";
$xmlDATA .= "</book>\n";
}
$xmlDATA .= "</books>\n";

echo $xmlDATA;
?>