>  기사  >  백엔드 개발  >  PHP:基于XML的在线小词典案例

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

WBOY
WBOY원래의
2016-07-30 13:31:021015검색
span>html> html lang="en"> head> meta charset="gb2312"> title>dictionarytitle> 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>

$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")."
"
; } } 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 "
BACK "
;


以上就介绍了PHP:基于XML的在线小词典案例,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.