本文章来给大家介绍一个PHP 操作xml编程之xml的crud操作,有需要了解的同学可参考.
html代码页面,代码如下:
- <html>
- <head>
- <meta http-equiv="Content-type" content="text/html;charset=utf-8">
- </head>
- <body>
- <form action="wordProcess.php" method="post">
- <input type="text" name="enword">
- <input type="hidden" name="type" value="query">
- <input type="submit" value="查询">
- </form>
- <span>添加单词</span>
- <form action="wordProcess.php" method="post">
- 英文:<input type="text" name="enword"><br>
- 中文:<input type="text" name="zhword"><br>
- <!--<input type="hidden" name="type" value="insert">
- <input type="hidden" name="type" value="update"> -->
- <input type="submit" name="type" value="添加">
- <input type="submit" name="type" value="修改">
- </form>
- <form action="wordProcess.php" method="post">
- <input type="text" name="word">
- <input type="hidden" name="type" value="delete">
- <input type="submit" value="删除">
- </form>
- </body>
- </html>
wordpress.php文件,代码如下:
- <?php
-
-
- $type=$_REQUEST['type'];
-
-
-
- $doc=new DOMDocument();
- $doc->load("words.xml");
-
-
- if($type=="query"){
-
- $enword=$_REQUEST['enword'];
-
-
- $isEnter=false;
-
- $words=$doc->getElementsByTagName("word");
-
- for($i=0;$i<$words->length;$i++){
- $word_node=$words->item($i);
-
- $en_word=getNodeVal($word_node,"en");
- $zh_word=getNodeVal($word_node,"zh");
-
- if($enword==$en_word){
- $isEnter=true;
- echo $enword."的中文意思是:".getNodeVal($word_node,"zh");
- echo "<br/><a href='wordView.php'>返回继续查询</a>";
- }else if($enword==$zh_word){
- $isEnter=true;
- echo $enword."的英文意思是:".getNodeVal($word_node,"en");
- echo "<br/><a href='wordView.php'>返回继续查询</a>";
- }
- }
-
- if(!$isEnter){
- echo "无法查询";
- echo "<br/><a href='wordView.php'>返回继续查询</a>";
- }
- }else if($type=="添加"){
-
- $enword=$_REQUEST['enword'];
- $zhword=$_REQUEST['zhword'];
- if(!emptyempty($enword)&&!emptyempty($zhword)){
-
- $root=$doc->getElementsByTagName("words")->item(0);
-
- $word=$doc->createElement("word");
- $en=$doc->createElement("en",$enword);
- $zh=$doc->createElement("zh",$zhword);
-
-
-
- $root->appendChild($word);
- $word->appendChild($en);
- $word->appendChild($zh);
-
-
- $doc->save("words.xml");
- echo "添加成功<br/><a href='wordView.php'>返回继续操作</a>";
- }else{
-
- echo "请输入单词";
- echo "<br/><a href='wordView.php'>返回继续操作</a>";
- exit();
- }
- }else if($type=="delete"){
-
- $word=$_REQUEST['word'];
-
- $words=$doc->getElementsByTagName("word");
- $isEnter=false;
-
- for($i=0;$i<$words->length;$i++){
- $word_node=$words->item($i);
-
- $en_word=getNodeVal($word_node,"en");
- $zh_word=getNodeVal($word_node,"zh");
-
- if($word==$en_word || $word==$zh_word){
- $isEnter=true;
-
- $word_node->parentNode->removeChild($word_node);
- $doc->save("words.xml");
- echo "删除成功<br/><a href='wordView.php'>返回继续操作</a>";
- }
- }
-
- if(!$isEnter){
- echo "操作失败";
- echo "<br/><a href='wordView.php'>返回继续操作</a>";
- }
- }else if($type="修改"){
-
- $enword=$_REQUEST['enword'];
- $zhword=$_REQUEST['zhword'];
- if(!emptyempty($enword)&&!emptyempty($zhword)){
-
- $words=$doc->getElementsByTagName("word");
-
- $isEnter=false;
- for($i=0;$i<$words->length;$i++){
- $word_node=$words->item($i);
-
- $en_word=getNodeVal($word_node,"en");
- $zh_word=getNodeVal($word_node,"zh");
-
- if($enword==$en_word && $zhword!=$zh_word){
-
- $isEnter=true;
-
- $zh=$word_node->getElementsByTagName("zh")->item(0);
- $zh->nodeValue=$zhword;
- $doc->save("words.xml");
- echo "修改成功";
- echo "<br/><a href='wordView.php'>返回继续操作</a>";
- }else if($enword!=$en_word && $zhword==$zh_word){
-
- $isEnter=true;
- $en=$word_node->getElementsByTagName("en")->item(0);
- $en->nodeValue=$enword;
- $doc->save("words.xml");
- echo "修改成功";
- echo "<br/><a href='wordView.php'>返回继续操作</a>";
- }
- }
-
- if(!$isEnter){
- echo "没有做任何修改";
- echo "<br/><a href='wordView.php'>返回继续操作</a>";
- }
-
- }else{
- echo "请输入需要修改的单词";
- echo "<br/><a href='wordView.php'>返回继续操作</a>";
- exit();
- }
- }
-
-
- function getNodeVal(&$MyNode,$tagName){
- return $MyNode->getElementsByTagName($tagName)->item(0)->nodeValue;
- }
- ?>
words.xml,代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <words><word><en>boy</en><zh>男孩</zh></word><word><en>girl</en><zh>女孩</zh></word><word><en>fire</en><zh>火</zh></word><word><en>word</en><zh>词库</zh></word></words>
|