PHP:基于XML的在线小词典案例

<!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 DOMDocument();
$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> ";


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值