php+xml實現(xiàn)在線英文詞典之添加詞條的方法
更新時間:2015年01月23日 15:26:40 投稿:shichen2014
這篇文章主要介紹了php+xml實現(xiàn)在線英文詞典之添加詞條的方法,接著上一篇的通過英文查詢漢字進一步完善了詞條的添加功能,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了php+xml實現(xiàn)在線英文詞典之添加詞條的方法。分享給大家供大家參考。具體如下:
接著上一篇《php+xml實現(xiàn)在線英文詞典查詢的方法》,這里要添加一個功能,提交英文單詞和中文意思,將這些信息添加到xml文檔中。
xml文件(數(shù)據(jù)庫):words.xml
復(fù)制代碼 代碼如下:
<?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>
<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ù)制代碼 代碼如下:
<h2 style="color:green">在線英漢詞典</h2>
<h4>查詢英文單詞</h4>
<form action="xmlprocess.php" method="post">
請輸入英文單詞:<input type="text" name="enword" />
<input type="submit" value="查詢" name="sub" />
</form>
<h4>添加英文單詞</h4>
<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>
<h4>查詢英文單詞</h4>
<form action="xmlprocess.php" method="post">
請輸入英文單詞:<input type="text" name="enword" />
<input type="submit" value="查詢" name="sub" />
</form>
<h4>添加英文單詞</h4>
<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對象
$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é)點
$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");
}
?>
//創(chuàng)建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'];
//獲取根節(jié)點
$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");
}
?>
希望本文所述對大家的php+XML程序設(shè)計有所幫助。
相關(guān)文章
php版交通銀行網(wǎng)銀支付接口開發(fā)入門教程
這篇文章主要介紹了php版交通銀行網(wǎng)銀支付接口開發(fā)方法,結(jié)合簡單實例形式分析了php操作交通銀行網(wǎng)銀接口的的開發(fā)步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-09-09PHP調(diào)用存儲過程返回值不一致問題的解決方法分析
這篇文章主要介紹了PHP調(diào)用存儲過程返回值不一致問題的解決方法,結(jié)合實例形式分析了存儲過程調(diào)用返回值不一致的原因與解決方法,需要的朋友可以參考下2016-04-04