名站导航为爱好php程序的朋友们提供php相关的教程知识。
XML文件一般情况下用来保存网站的数据信息,比如网站会员信息等。XML文件数据在整个网站中可能要用于程序中。所以,用PhP程序来读取XML文件中的数据是一个PhP程序程序员必备的知识。上一篇文章我们有介绍PhP程序读取XML文件中的数据,这里我们来看一个PhP程序读取XML文件的具体实例操作。
下面是XML文件具体代码如下:
library.xml:
<?xml version="1.0" encoding="gb2312"?> <root> <groups> <group gid="1">super</group> <group gid="2">admin</group> <group gid="3">change</group> <group gid="4">program</group> </groups> <users> <user> <name>Apache 2</name> <author>Peter Wainwright</author> <publisher>Wrox</publisher> <group>1</group> </user> <user> <name>Advanced PhP程序 Programming</name> <author>George Schlossnagle</author> <publisher>Developer Library</publisher> <group>1</group> <group>3</group> </user> <user> <name>Visual FoxPro 6 - Programmers Guide</name> <author>Eric Stroo</author> <publisher>Microsoft Press</publisher> <group>2</group> </user> <user> <name>Mastering Java 2</name> <author>John Zukowski</author> <publisher>Sybex</publisher> <group>4</group> </user> </users> </root>
readlibrary.php:
<?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; } ?> <html> <head> <title>XML Library</title> </head> <body> <? 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); ?> <!-- <div> <h2><?php echo($name) ?></h2> <b>Author:</b>: <?php echo($author) ?></br> <b>group: </b>: <?php echo($catList) ?></br> </div> --> <?php //echo($name ." - " . $author. " - ". $catList ."<br>\n" ); echo($name ." || " . $catList ."<br>\n" ); endforeach; ?> </html>
writelibrary.php:
<?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 ); } ?>
好了关于php程序的知识就说到这里希望可以帮助需要的朋友。,使用PhP程序生成XML文件具体代码如下
XML文件在一个网站中可以说是必不可少的,PhP程序将数据保存到XML文件都要对XML文件的操作。例如,打开XML文件、将数据写入XML文件等,这里我们使用PhP程序来生成我们想要的XML文件具体实例操作具体代码如下。