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

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 );
}
?>

有用  |  无用

猜你喜欢