温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在php中使用xml实现在线英文词典之添加词条

发布时间:2021-02-07 20:26:44 来源:亿速云 阅读:101 作者:Leah 栏目:开发技术

本篇文章为大家展示了如何在php中使用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>teacher</en>
 <ch>老师</ch>
</word>
<word>
 <en>beauty</en>
 <ch>美女</ch>
</word>
</words>

查询与添加文件:words.php

复制代码 代码如下:

<h3 >在线英汉词典</h3>
<h5>查询英文单词</h5>
<form action="xmlprocess.php" method="post">
请输入英文单词:<input type="text" name="enword" />
<input type="submit" value="查询" name="sub" />
</form>
<h5>添加英文单词</h5>
<form action="xmlprocess.php" method="post">
英文单词:<input type="text" name="en_word" /><br />
中文意思:<input type="text" name="ch_word" />
<input type="submit" value="添加" name="add">
</form>

处理文件:xmlprocess.php

复制代码 代码如下:

<?php
//创建xml对象
$xmldoc = new DOMDocument();
$xmldoc->load("words.xml");
//查询
if(!empty($_POST['sub'])){
 $en_word = $_POST['enword'];
 $word = $xmldoc->getElementsByTagName("en");
 for($i=0;$i<$word->length;$i++){
  if($en_word==$word->item($i)->nodeValue){
   $cn_word = $xmldoc->getElementsByTagName("ch")->item($i)->nodeValue;
   break;
  }else{
   $cn_word = "找不到你所输入的单词";
  }
 }
 echo $cn_word;
}
//增加词条
if(!empty($_POST['add'])){
 $en_word = $_POST['en_word'];
 $ch_word = $_POST['ch_word'];
 //获取根节点
 $words = $xmldoc->getElementsByTagName("words")->item(0);
 //增加元素,并添加内容
 $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);
 $words->appendChild($new_word);
 //保存
 $xmldoc->save("words.xml");
}
?>

上述内容就是如何在php中使用xml实现在线英文词典之添加词条,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI