操作的xml文档:books.xml
<?xml version="1.0" encoding="utf-8"?>
<books>
<book type="脚本语言">
<name>php</name>
</book>
<book type="标记语言">
<name>html</name>
</book>
<book type="编译型语言">
<name>c#</name>
</book>
</books>
显示功能代码和页面:books_admin.php
<?php
echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';
$doc = new DOMDocument();
$doc->load('books.xml');
$book = $doc->getElementsByTagName('book');
?>
<a href="books_add.php">添加图书</a>
<table width="300" border="1" align="center">
<tr>
<th>书名</th><th>类别</th><th>修改</th><th>删除</th>
</tr>
<?php
for($i= 0; $i < $book->length; $i++){
?>
<tr>
<td><?php echo $book->item($i)->nodeValue; ?></td>
<td><?php echo $book->item($i)->getAttribute('type'); ?></td>
<td><input type="button" value="修改" οnclick="location.href='books_modify.php?index=<?php echo $i; ?>'"></td>
<td><input type="button" value="删除" οnclick="if(confirm('确定要删除吗')) location.href='books_del.php?index=<?php echo $i; ?>'"></td>
</tr>
<?php
}
?>
</table>
添加图书代码和页面:books_add.php
<?php
echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';
if($_POST){
$doc = new DOMDocument();
$doc->preserveWhiteSpace=false;
$doc->formatOutput=true;
$path = './books.xml';
$doc->load($path);
$book = $doc->createElement('book');
$name = $doc->createElement('name', $_POST['name']);
$book->appendChild($name);
$book->setAttribute('type', $_POST['type']);
$books = $doc->documentElement;
$books->appendChild($book);
if($doc->save($path)){
header('location:books_admin.php');
} else {
echo '添加失败!!';
}
}
?>
<form id="form1" name="form1" method="post" action="books_add.php">
<table width="400" border="1" align="center">
<tr>
<td colspan="2" align="center">添加图书</td>
</tr>
<tr>
<td>书名:</td>
<td><input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td>类别:</td>
<td>
<select name="type" id="type">
<option value="脚本语言">脚本语言</option>
<option value="动态语言">动态语言</option>
<option value="静态语言">静态语言</option>
<option value="标记语言">标记语言</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input name="button" type="submit" id="button" value="提交"/><input type="button" name="button2" id="button2" value="返回" οnclick="location.href='books_admin.php'"></td>
</tr>
</table>
</form>
修改图书代码和页面:books_modify.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<?php
$index = $_GET['index'];
echo $index;
if(!is_numeric($index)){
die('非法操作');
}
$doc = new DOMDocument();
$doc->preserveWhiteSpace=false;
$path = './books.xml';
$doc->load($path);
$oldbook = $doc->getElementsByTagName('book')->item($index);
if($_POST){
$newbook = $doc->createElement('book');
$name = $doc->createElement('name', $_POST['name']);
$newbook->setAttribute('type', $_POST['type']);
$newbook->appendChild($name);
$root-$doc->documentElement->replaceChild($newbook, $oldbook);
$doc->save($path);
header('location:books_admin.php');
}
?>
<form id="form1" name="form1" method="post" action="books_modify.php?index=<?php echo $index; ?>">
<table width="400" border="1" align="center">
<tr>
<td colspan="2" align="center">修改图书</td>
</tr>
<tr>
<td>书名:</td>
<td>
<input type="text" name="name" id="name" value="<?php echo $oldbook->nodeValue; ?>" />
</td>
</tr>
<tr>
<td>类别:</td>
<td>
<select name="type" id="type">
<option value="<?php echo $oldbook->getAttribute('type'); ?>"><?php echo $oldbook->getAttribute('type'); ?></option>
<option value="脚本语言">脚本语言</option>
<option value="动态语言">动态语言</option>
<option value="静态语言">静态语言</option>
<option value="标记语言">标记语言</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input name="button" type="submit" id="button" value="修改"/><input type="button" name="button2" id="button2" value="返回" οnclick="location.href='books_admin.php'"></td>
</tr>
</table>
</form>
</body>
</html>
删除功能代码和页面:books_del.php
<?php
$doc = new DOMDocument();
$doc->preserveWhiteSpace=false;
$doc->formatOutput=true;
$doc->load('books.xml');
$index = $_GET['index'];
$book = $doc->getElementsByTagName('book')->item($index);
$parent = $book->parentNode->removeChild($book);
$doc->save('books.xml');
header('location:books_admin.php');