文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>php读写xml一例

php读写xml一例

时间:2008-08-04  来源:pamire

library.xml:


  super
   admin
    change
    program

  
   
     Apache 2
     Peter Wainwright
     Wrox
     1
   
   
     Advanced PHP Programming
     George Schlossnagle
     Developer Library
     1
     3
   
   
     Visual FoxPro 6 - Programmers Guide
     Eric Stroo
     Microsoft Press
     2
   
   
     Mastering Java 2
     John Zukowski
     Sybex
     4
   

/**********************************************/
readlibrary.php:
load( 'library.xml' );
  $groups = array();
  $XMLGroups = $xml->getElementsByTagName('groups')->item(0);
  foreach($XMLGroups->getElementsByTagName('group') as $groupNode) {
    /*注意我们是如何得到属性的*/
    $gid = $groupNode->getAttribute('gid');
    $groups[$gid] = $groupNode->firstChild->nodeValue;
  }
?>


XML Library


getElementsBytagName('user') as $user):
   $name = $user->getElementsByTagName('name')->item(0)->firstChild->nodeValue;
   $author = $user->getElementsByTagName('author')->item(0)->firstChild->nodeValue;
   $userCategories = $user->getElementsByTagName('group');
   $catList = '';
   foreach($userCategories as $category) {
     $catList .= $groups[$category->firstChild->nodeValue] . ', ';
   }
   $catList = substr($catList, 0, -2); ?>
  
  
Author::  
group: :  
  
-->
  

/************************************/
writelibrary.php:
formatOutput = true;
$r = $doc->createElement( "root" );
$doc->appendChild( $r );
setGroup();
setUser();
echo $doc->saveXML();
$doc->save("book1.xml");
function setGroup()
{
global $doc, $r;

  $groups = array();
  $groups [] = array(
  'id' => '1',
  'name' => 'Jack',
  );
  $groups [] = array(
  'id' => '2',
  'name' => 'Herrington',
  );
  $groups [] = array(
  'id' => '3',
  'name' => 'Hello',
  );
  
$b = $doc->createElement( "groups" );
   
  foreach( $groups as $group )
  {
   $grp = $doc->createElement( "group" );
   $grp->appendChild( $doc->createTextNode( $group['name'] ) );
   
   // create attribute node
  $id = $doc->createAttribute("gid");
  $grp->appendChild($id);
  $idValue = $doc->createTextNode( $group['id'] );
  $id->appendChild($idValue);
   $b->appendChild( $grp );
  }
$r->appendChild( $b );  
  
}
function setUser()
{
global $doc, $r;

$users [] = array(
  'title' => 'PHP Hacks',
  'author' => 'Jack Herrington',
  'publisher' => "O'Reilly"
  );
  $users [] = array(
  'title' => 'Podcasting Hacks',
  'author' => 'Jack Herrington',
  'publisher' => "O'Reilly"
  );
  
  $u = $doc->createElement( "users" );
  $doc->appendChild( $u );
  
  foreach( $users as $user )
  {
   $b = $doc->createElement( "user" );
   
   $author = $doc->createElement( "author" );
   $author->appendChild( $doc->createTextNode( $user['author'] ) );
   $b->appendChild( $author );
   
   $title = $doc->createElement( "title" );
   $title->appendChild( $doc->createTextNode( $user['title'] ) );
   $b->appendChild( $title );
   
   $publisher = $doc->createElement( "publisher" );
   $publisher->appendChild( $doc->createTextNode( $user['publisher'] ) );
   $b->appendChild( $publisher );
   
   $u->appendChild( $b );
  }
  $r->appendChild( $u );
}  
?>  

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载