xml增删改查

Posted on Posted in php
Tips: 本文创建于2014年5月8日,已超过 2 年,内容或图片可能已经失效!

目录

list.php

[code lang="php"]
<?php
require './studentModel.class.php';
$studentModel = new studentModel ();
$students = $studentModel->getList ();
?>
<a href="add.php">添加</a>
<table border='1'>
<tr>
<th>ID</th>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>操作</th>
</tr>
<?php foreach ($students as $student):?>
<tr>
<td><?php echo $student->id?></td>
<td><?php echo $student->sn?></td>
<td><?php echo $student->name?></td>
<td><?php echo $student->sex?></td>
<td><?php echo $student->age?></td>
<td><a href="remove.php?id=<?php echo $student->id?>">删除</a> <a
href="add.php?id=<?php echo $student->id?>">编辑</a></td>
</tr>
<?php endforeach;?>
</table>
[/code]

remove

[code lang="php"]
<?php
/*
* 接受要删除的ID 引入类
*/
$id = $_GET ['id'];
require_once './studentModel.class.php';
$studentModel = new studentModel ();
$studentModel->remove ( $id );

header ( 'Location: list.php' );
[/code]

add

[code lang="php"]
<?php
// 判断当前请求方式post
if ($_SERVER ['REQUEST_METHOD'] == 'POST') {
$student = ( object ) $_POST;
require_once './studentModel.class.php';
$studentModel = new studentModel ();

if (empty ( $student->id )) {
$studentModel->add ( $student );
} else {
$studentModel->update ( $student );
}
header ( 'Location: list.php' );
} else {
if (! empty ( $_GET ['id'] )) {
require './studentModel.class.php';
$studentModel = new studentModel ();
$student = $studentModel->get ( $_GET ['id'] );
}
}
?>
<form action="add.php" method="post">
<input type="hidden" name="id"
value="<?php echo isset($student)?$student->id:"" ?>" /> 学号:<input
type="text" name="sn"
value="<?php echo isset($student)?$student->sn:"" ?>"><br /> 姓名:<input
type="text" name="name"
value="<?php echo isset($student)?$student->name:"" ?>"><br /> 年龄:<input
type="text" name="age"
value="<?php echo isset($student)?$student->age:"" ?>"><br /> 性别:<input
type="text" name="sex"
value="<?php echo isset($student)?$student->sex:"" ?>"><br /> <input
type="submit" value="保存" />
</form>
[/code]

studentModel

[code lang="php"]
<?php
header ( 'Content-Type:text/html; charset=utf-8' );
class studentModel {
/**
* 更新数据
*/
public function update($student) {
$dom = $this->load ();
// 创建元素
$studentElement = $dom->createElement ( 'student' );
// 增加属性
$studentElement->setAttribute ( 'sn', $student->sn );
$studentElement->setAttribute ( 'id', $student->id );
// 创建name,age,sex元素,放到student元素下面
$studentElement->appendChild ( $dom->createElement ( 'name', $student->name ) );
$studentElement->appendChild ( $dom->createElement ( 'age', $student->age ) );
$studentElement->appendChild ( $dom->createElement ( 'sex', $student->sex ) );

$studentNodeList = $dom->getElementsByTagName ( 'student' );
foreach ( $studentNodeList as $studentNode ) {
if ($studentNode->getAttribute ( 'id' ) == $student->id) {
// 替换
$studentNode->parentNode->replaceChild ( $studentElement, $studentNode );
break;
}
}
$dom->save ( './students.xml' );
}
/**
* 根据id的值找到内容
*/
public function get($id) {
$dom = $this->load ();
$studentNodeList = $dom->getElementsByTagName ( 'student' );
foreach ( $studentNodeList as $studentNode ) {
if ($studentNode->getAttribute ( 'id' ) == $id) {
return $this->element2object ( $studentNode );
}
}
}
/**
* 添加数据
*/
public function add($student) {
$dom = $this->load ();
// 创建一个新元素
$studentElement = $dom->createElement ( 'student' );
// 创建新属性
$studentElement->setAttribute ( 'sn', $student->sn );
$studentElement->setAttribute ( 'id', uniqid () );
// 创建节点
$nameElement = $dom->createElement ( 'name' );
$ageElement = $dom->createElement ( 'age' );
$sexElement = $dom->createElement ( 'sex' );
// CDATA 处理
$nameElement->appendChild ( $dom->createCDATASection ( $student->name ) );
$ageElement->appendChild ( $dom->createCDATASection ( $student->age ) );
$sexElement->appendChild ( $dom->createCDATASection ( $student->sex ) );
// 把节点放到student下面
$studentElement->appendChild ( $nameElement );
$studentElement->appendChild ( $ageElement );
$studentElement->appendChild ( $sexElement );
// 把student放入到跟节点下面
$dom->documentElement->appendChild ( $studentElement );

$dom->save ( './students.xml' );
}
/**
* 接受ID,删除元素
*/
public function remove($id) {
$dom = $this->load ();
// 根据id找到元素
$studentNodeList = $dom->getElementsByTagName ( 'student' );
foreach ( $studentNodeList as $studentNode ) {
if ($studentNode->getAttribute ( 'id' ) == $id) {
$studentNode->parentNode->removeChild ( $studentNode );
break;
}
}
$dom->save ( './students.xml' );
}
/**
* 加载一个xml @return DOMDocument
*/
private function load() {
$dom = new DOMDocument ( '1.0', 'utf-8' );
if (! @$dom->load ( './students.xml' )) {
$dom->appendChild ( $dom->createElement ( 'students' ) );
}
return $dom;
}
/**
* 获取xml中的每一个元素值
*/
public function getList() {
$students = array ();
$dom = $this->load ();
// 获取student节点元素
$studentNodeList = $dom->getElementsByTagName ( 'student' );
foreach ( $studentNodeList as $studentNode ) {
$student = $this->element2object ( $studentNode );
$students [] = $student;
}
return $students;
}
/**
* 将student元素变成对象
*
* @param unknown $studentNode
* @return stdClass
*/
public function element2object($studentNode) {
// 获取属性
$sn = $studentNode->getAttribute ( 'sn' );
$id = $studentNode->getAttribute ( 'id' );
// 获取子节点,再获取文本内容
$name = $studentNode->getElementsByTagName ( 'name' )->item ( 0 )->textContent;
$age = $studentNode->getElementsByTagName ( 'age' )->item ( 0 )->textContent;
$sex = $studentNode->getElementsByTagName ( 'sex' )->item ( 0 )->textContent;
$student = new stdClass ();
$student->sn = $sn;
$student->id = $id;
$student->name = $name;
$student->age = $age;
$student->sex = $sex;
return $student;
}
}
[/code]

» 转载请注明来源:若我若鱼 » xml增删改查

Leave a Reply

Your email address will not be published. Required fields are marked *

7 + 5 =