首页 > 开发 > PHP > 正文

php生成xml文件

2024-05-04 21:58:41
字体:
来源:转载
供稿:网友
  1. <?php 
  2.   $books = array(); 
  3.   $books [] = array
  4.   'title' => 'PHP Hacks'
  5.   'author' => 'Jack Herrington'
  6.   'publisher' => "O'Reilly" 
  7.   ); 
  8.   $books [] = array
  9.   'title' => 'Podcasting Hacks'
  10.   'author' => 'Jack Herrington'
  11.   'publisher' => "O'Reilly" 
  12.   ); 
  13.   //开源代码Vevb.com 
  14.   $doc = new DOMDocument(); 
  15.   $doc->formatOutput = true; 
  16.    
  17.   $r = $doc->createElement( "books" ); 
  18.   $doc->appendChild( $r ); 
  19.    
  20.   foreach$books as $book ) 
  21.   { 
  22.   $b = $doc->createElement( "book" ); 
  23.    
  24.   $author = $doc->createElement( "author" ); 
  25.   $author->appendChild( 
  26.   $doc->createTextNode( $book['author'] ) 
  27.   ); 
  28.   $b->appendChild( $author ); 
  29.    
  30.   $title = $doc->createElement( "title" ); 
  31.   $title->appendChild( 
  32.   $doc->createTextNode( $book['title'] ) 
  33.   ); 
  34.   $b->appendChild( $title ); 
  35.    
  36.   $publisher = $doc->createElement( "publisher" ); 
  37.   $publisher->appendChild( 
  38.   $doc->createTextNode( $book['publisher'] ) 
  39.   ); 
  40.   $b->appendChild( $publisher ); 
  41.    
  42.   $r->appendChild( $b ); 
  43.   } 
  44.    
  45.   echo $doc->saveXML(); 
  46. ?> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表