一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php生成xml文件

时间:2008-05-24 编辑:简简单单 来源:一聚教程网

 代码如下 复制代码

  $books = array();
  $books [] = array(
  'title' => 'PHP Hacks',
  'author' => 'Jack Herrington',
  'publisher' => "O'Reilly"
  );
  $books [] = array(
  'title' => 'Podcasting Hacks',
  'author' => 'Jack Herrington',
  'publisher' => "O'Reilly"
  );
 
  $doc = new DOMDocument();
  $doc->formatOutput = true;
 
  $r = $doc->createElement( "books" );
  $doc->appendChild( $r );
 
  foreach( $books as $book )
  {
  $b = $doc->createElement( "book" );
 
  $author = $doc->createElement( "author" );
  $author->appendChild(
  $doc->createTextNode( $book['author'] )
  );
  $b->appendChild( $author );
 
  $title = $doc->createElement( "title" );
  $title->appendChild(
  $doc->createTextNode( $book['title'] )
  );
  $b->appendChild( $title );
 
  $publisher = $doc->createElement( "publisher" );
  $publisher->appendChild(
  $doc->createTextNode( $book['publisher'] )
  );
  $b->appendChild( $publisher );
 
  $r->appendChild( $b );
  }
 
  echo $doc->saveXML();
  ?>

热门栏目