xml学习笔记④PHP DOM--增删改查综合案例演示

7 篇文章 1 订阅

用xml文件做词库,编写一个在线英汉词典,使用phpdom技术实现增删改查词条
1.分析并作出一个程序框架图(MFC结构)
2.三个文件wordView.php wordprocess.php words.xml

对应代码:
wordView.php

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>词典翻译</title>
</head>
<body>
<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>
<form action="wordprocess.php" method="post">
<h1>增加单词</h1>
<form action="wordprocess.php" method="post">
请输入英文单词:<input type="text" name="enword"><br><br>
请输入中文单词:<input type="text" name="chword">
<input type="hidden" name="type" value="add">
<input type="submit" value="增加">
</form>
<form action="wordprocess.php" method="post">
<h1>修改单词</h1>
<form action="wordprocess.php" method="post">
请输入英文单词:<input type="text" name="enword"><br><br>
请输入中文单词:<input type="text" name="chword">
<input type="hidden" name="type" value="set">
<input type="submit" value="修改">
</form>
<form action="wordprocess.php" method="post">
<h1>删除单词</h1>
<form action="wordprocess.php" method="post">
请输入英文单词:<input type="text" name="enword"><br><br>
请输入中文单词:<input type="text" name="chword">
<input type="hidden" name="type" value="del">
<input type="submit" value="删除">
</form>
</body>
</html>

wordprocess.php

<?php
    //接收用户提交的单词
    $type=$_REQUEST['type'];
    //创建DOCDocment
    $xmldoc=new DOMDocument();
    //加载xml文件
    $xmldoc->load("words.xml");
//************查询单词********************
        if ($type=="query"){
            //接收新的单词
            $newword=$_REQUEST['enword'];
            //查询xml
            $words=$xmldoc->getElementsByTagName("word");
            //看是否在xml中找得到
            $lisenter=false;
            //遍历
            for ($i=0;$i<$words->length;$i++){
                $word=$words->item($i);
                $word_en=getnodeval($word,"en");
                if ($newword==$word_en){
                    $lisenter=true;
                    echo $newword."--对应的中文是--".getnodeval($word,"ch").'<br>';
                }
            }
            //查不到怎么办
            if (!$lisenter){
                echo "查不到".'<br>';
            }
        }
        function getnodeval(&$node, $tagname){
            return $node->getElementsByTagName($tagname)->item(0)->nodeValue;
        }
//********************增加单词****************
        if ($type=="add"){
            //echo "add";
            //接收用户输入的中文和英文
            $en_word=$_REQUEST['enword'];
            $ch_word=$_REQUEST['chword'];
            //echo $new_en_word.$new_ch_word;
            //得到根元素
        $root=$xmldoc->getElementsByTagName("words")->item(0);
            //创建word节点
            $new_word=$xmldoc->createElement("word");
            //创建英文节点
            $new_word_en=$xmldoc->createElement("en");
            //把输入进来的英文单词放到英文节点
            $new_word_en->nodeValue=$en_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);
            $a=$xmldoc->save("words.xml");
            if (!$a){
                echo "添加失败";
            }else {
                echo "添加成功";
            }
        }

words.xml

<?xml version="1.0" encoding="UTF-8"?>
<words>
    <word>
        <en>boy</en>
        <ch>男孩</ch>
    </word>
    <word>
        <en>girl</en>
        <ch>女孩</ch>
    </word>
    <word>
        <en>student</en>
        <ch>学生</ch>
    </word>
    <word>
        <en>college</en>
        <ch>学校</ch>
    </word>
</words>

注:只写了查阅和增加的案例

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值