PHP读取XML文件数据实例
作者:bea
XML文件一般情况下用来保存网站的数据信息,比如网站会员信息等。XML文件数据在整个网站中可能要用于程序中。所以,用PHP来读取XML文件中的数据是一个PHP程序员必备的知识。上一篇文章我们有介绍PHP读取XML文件中的数据,这里我们来看一个PHP读取XML文件的实例。
下面是XML文件代码:
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); ?>
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 );
}
?>
猜你喜欢
您可能感兴趣的文章:
- PHP cookie实现访客访问次数的统计
- PHP图像各种处理代码图像处理类
- 正则表达式匹配字符串中图片
- php代码获得电脑上网网卡MAC地址
- PHP实例以sql文件导入导出mysql数据库
- 分享一个PHP写的计算页面执行时间类
- 使用PHP计算出页面的执行时间
- PHP cookie实例制作一个购物车
- PHP学习之cookie使用详情
- 详解PHP预定义超全局数组变量$_SERVER
- PHP中memcache与memcached对比
- PHP编程中如何使用memcached
- PHP错误控制运算符@的使用
- PHP引用字符串单引号与双引号的区别
- PHP MVC制作一个简单的留言板
- PHP学习:联合MySQL动态创建XML
- 分享一个PHP操作XML类DOMDocument
- PHP解析XML数据获取最新RSS
- 使用PHP生成XML文件代码