PHP:基于XML的在线小词典案例
2015-08-20 18:06
811 查看
<!DOCTYPE html> <html lang="en"> <head> <meta charset="gb2312"> <title>dictionary</title> </head> <img src="view.jpg" width="400px" /> <h1>查询单词</h1> <form action="wordProcess.php" method="post" > 请输入单词:<input type="text" name="enword" /> <input type="hidden" name="type" value="query"> <input type="submit" value="查询"> </form> <h1>添加单词</h1> <form action="wordProcess.php" method="post"> 请输入英文:<input type="text" name="enword" /><br> 请输入中文:<input type="text" name="chword" /><br> <input type="hidden" name="type" value="add" /> <input type="submit" value="添加"> </form></html>
<?php
$type=$_POST['type'];
$xmlDoc=new DOMDoc
4000
ument();
$xmlDoc->load("words.xml");
if($type=="query"){
$query_word=$_POST['enword'];
$words=$xmlDoc->getElementsByTagName("word");
$isEnter=false;
for($i=0;$i<$words->length;$i++){
$word=$words->item($i);
$word_en=getNodeVal($word,"en");
if($query_word==$word_en){
$isEnter=true;
echo $query_word."--中文意思:".getNodeVal($word,"ch")."<br>";
}
}
if(!$isEnter){
echo "NOT FOUNDED";
}
}else if($type=="add"){
$eng_word=$_POST['enword'];
$ch_word=$_POST['chword'];
$root=$xmlDoc->getElementsByTagName("words")->item(0);
$new_word=$xmlDoc->createElement("word");
$new_word_en=$xmlDoc->createElement("en");
$new_word_en->nodeValue=$eng_word;
$new_word_ch=$xmlDoc->createElement("ch");
$new_word_ch->nodeValue=$ch_word;
$new_word->appendChild($new_word_en);
$new_word->appendChild($new_word_ch);
$root->appendChild($new_word);
$b=$xmlDoc->save("words.xml");
if(!$b){
echo "添加失败";
}else{
echo "添加成功";
}
}
function getNodeVal(&$mynode,$tagname){
return $mynode->getElementsByTagName($tagname)->item(0)->nodeValue;
}
echo "<br><a href='wordView.php'>BACK</a> ";
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- XML 与 JSON 优劣对比
- Php Installing An Expansion
- As3.0 xml + Loader应用代码
- 网马生成器 MS Internet Explorer XML Parsing Buffer Overflow Exploit (vista) 0day
- ext读取两种结构的xml的代码
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- C#针对xml基本操作及保存配置文件应用实例