您当前的位置: 首页 > 学无止境 > XML与XSLT 网站首页XML与XSLT
xml与php经典实例-金山词霸xml版(最精简版)2.0
发布时间:2015-04-09 07:37:19编辑:雪饮阅读()
只是在上一版本基础上添加了增加词条的功能:
html源码(index.html):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<!--
实现思路分析:
金山词霸不仅有查询单词功能、还有新增词条功能。所以我们需要两个表单,第一个表单提交一个隐藏值为查询的标识
第二个表单提交一个隐藏的值为新增词条的标识,然后通过后端php来判断用户的行为是要查询还是要新增。这里暂时只写一个
查询的表单,新增表单依次类推。
-->
<img src="jscb.jpg"/>
<form action="ciba.php" method="post">
请输入单词:<input type="text" name="enword"></input>
<input type="hidden" name="type" value="query">
<input type="submit" value="查询"></input>
</form>
<br/>
<p>雪饮个人博客自己收录的词条有:boy,girl,school,Snow drink,Personal blog</p>
<p>添加词条</p>
<form action="ciba.php" method="post">
请输入英文单词:<input type="text" name="enword"></input>
请输入中文解释:<input type="text" name="cnword"></input>
<input type="hidden" name="type" value="tianjia">
<input type="submit" value="添加词条"></input>
</form>
</body>
</html>
php源码(ciba.php):
<?php
//接收表单提交的隐藏值
$type=$_POST["type"];
//无论用户是想要查询或者新增都需要经过xml,所以先将xml载入
$xmldoc=new DOMDocument();
$xmldoc->load("ciku.xml");
//我们需要自定义一个函数为了在进行遍历取值的时候方便调用
function quzhi($mydone,$tagname){
return $mydone->getElementsByTagName($tagname)->item(0)->nodeValue;
}
//判断用户想要执行的操作
if($type=="query"){
//查询xml需要和用户提交的单词进行比对,所以此处就需要接收到用户提交过来的单词的值
$yonghudanci=$_POST["enword"];
//由于词库所存单词有限所以不是任何单词都可以查到的,既是如此我们需要判断词库是否查到用户检索的单词
$isenter=false;
/*
由于每个词都有中英两种,并且我们并不知道用户所要查询的单词是在哪一个“word”标签之下(每个“word”标签之下都有一个词的中英结果),所以我们只能取出一个“word”节点列表,对该节点列表进行遍历查询,直到某个节点的子节点“en”标签的值符合用户输入的值时我们就将该节点的“cn”标签值输出(暂时只做英译汉的单向查询)
*/
$words=$xmldoc->getElementsByTagName("word");//取得“word”节点列表
for($i=0;$i<$words->length;$i++){
$word=$words->item($i);//取出本轮循环的索引所对应的中英词组
$word_english=quzhi($word,"en");
if($word_english==$yonghudanci){
$isenter=true;
echo $yonghudanci."的中文意思是:".quzhi($word,"cn")."<br/>";
}
}
//for循环结束之后若isenter仍然是false那么显然没有查到单词
if(!$isenter){
echo "很抱歉无此词条,雪饮个人博客是高仿金山词霸精简版的,所以词库没有词霸那边多,劳烦各位帮忙新增词条哦!<br/>";
}
}
else if($type=="tianjia"){
$en_word=$_POST["enword"];
$cn_word=$_POST["cnword"];
$root=$xmldoc->getElementsByTagName("words")->item(0);
$new_word=$xmldoc->createElement("word");
$new_word_en=$xmldoc->createElement("en");
$new_word_en->nodeValue=$en_word;
$new_word_cn=$xmldoc->createElement("cn");
$new_word_cn->nodeValue=$cn_word;
$new_word->appendChild($new_word_en);
$new_word->appendChild($new_word_cn);
$root->appendChild($new_word);
$save=$xmldoc->save("ciku.xml");
if($save){
echo "添加成功";
}
else{
echo "添加失败";
}
}
echo "<a href='/'>返回首页</a>";
?>
xml源码(ciku.xml):
<?xml version="1.0" encoding="utf-8"?>
<words>
<word>
<en>boy</en>
<cn>男孩</cn>
</word>
<word>
<en>girl</en>
<cn>女孩</cn>
</word>
<word>
<en>school</en>
<cn>学校</cn>
</word>
<word>
<en>Snow drink</en>
<cn>雪饮</cn>
</word>
<word>
<en>Personal blog</en>
<cn>个人博客</cn>
</word>
<word>
<en>Snow drink personal blog</en>
<cn>雪饮个人博客</cn>
</word>
</words>
关键字词:金山词霸xml版,xml经典实例,个人博客