如何在php中使用xml實(shí)現(xiàn)在線英文詞典之添加詞條-創(chuàng)新互聯(lián)

本篇文章為大家展示了如何在php中使用xml實(shí)現(xiàn)在線英文詞典之添加詞條,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站制作、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元嘉蔭做網(wǎng)站,已為上家服務(wù),為嘉蔭各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792

代碼如下:


<?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


復(fù)制代碼 代碼如下:

<h3 >在線英漢詞典</h3>
<h5>查詢英文單詞</h5>
<form action="xmlprocess.php" method="post">
請(qǐng)輸入英文單詞:<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


復(fù)制代碼 代碼如下:

<?php
//創(chuàng)建xml對(duì)象
$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'];
 //獲取根節(jié)點(diǎn)
 $words = $xmldoc->getElementsByTagName("words")->item(0);
 //增加元素,并添加內(nèi)容
 $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");
}
?>

上述內(nèi)容就是如何在php中使用xml實(shí)現(xiàn)在線英文詞典之添加詞條,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前文章:如何在php中使用xml實(shí)現(xiàn)在線英文詞典之添加詞條-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://bm7419.com/article36/hsjsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)、面包屑導(dǎo)航、響應(yīng)式網(wǎng)站網(wǎng)站營(yíng)銷、GoogleChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站