Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2660194
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类:

2008-08-04 13:28:35

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:

 $xml = new DOMDocument('1.0');
  $xml->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


  foreach($xml->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); ?>
 
  //echo($name ." - " . $author. " - ". $catList ."
\n" );
 echo($name ." || " . $catList ."
\n" );
  endforeach; ?>

/************************************/
writelibrary.php:

$doc = new DOMDocument();
$doc->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 );

?> 
阅读(1632) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~