当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP 操作xml编程之xml的crud操作

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-20 13:35:35 浏览: 评论:0 

本文章来给大家介绍一个PHP 操作xml编程之xml的crud操作,有需要了解的同学可参考.

html代码页面,代码如下:

  1. <html> 
  2. <head> 
  3.     <meta http-equiv="Content-type" content="text/html;charset=utf-8"> 
  4. </head> 
  5. <body> 
  6.     <form action="wordProcess.php" method="post"> 
  7.         <input type="text" name="enword"> 
  8.         <input type="hidden" name="type" value="query"> 
  9.         <input type="submit" value="查询"> 
  10.     </form> 
  11.     <span>添加单词</span> 
  12.     <form action="wordProcess.php" method="post"> 
  13.         英文:<input type="text" name="enword"><br> 
  14.         中文:<input type="text" name="zhword"><br> 
  15.         <!--<input type="hidden" name="type" value="insert"> 
  16.         <input type="hidden" name="type" value="update"> --> 
  17.         <input type="submit" name="type" value="添加"> 
  18.         <input type="submit" name="type" value="修改"> 
  19.     </form> 
  20.     <form action="wordProcess.php" method="post"> 
  21.         <input type="text" name="word"> 
  22.         <input type="hidden" name="type" value="delete"> 
  23.         <input type="submit" value="删除"> 
  24.     </form> 
  25. </body> 
  26. </html> 

wordpress.php文件,代码如下:

  1. <?php 
  2.  
  3.     //接收类型 看看用户做什么(查询、添加....) 
  4.     $type=$_REQUEST['type']; 
  5.     //echo $type; 
  6.     //exit(); 
  7.     //创建xml文档对象 
  8.     $doc=new DOMDocument(); 
  9.     $doc->load("words.xml"); 
  10.  
  11.     //进行判断 
  12.     if($type=="query"){ 
  13.         //获取用户输入的值 
  14.         $enword=$_REQUEST['enword']; 
  15.          
  16.         //判断是否进入查询 
  17.         $isEnter=false; 
  18.         //获取所有单词节点 
  19.         $words=$doc->getElementsByTagName("word"); 
  20.         //遍历单词节点 
  21.         for($i=0;$i<$words->length;$i++){ 
  22.             $word_node=$words->item($i); 
  23.             //获取不同的语种 
  24.             $en_word=getNodeVal($word_node,"en"); 
  25.             $zh_word=getNodeVal($word_node,"zh"); 
  26.             //查询 
  27.             if($enword==$en_word){ 
  28.                 $isEnter=true; 
  29.                 echo $enword."的中文意思是:".getNodeVal($word_node,"zh"); 
  30.                 echo "<br/><a href='wordView.php'>返回继续查询</a>"
  31.             }else if($enword==$zh_word){ 
  32.                 $isEnter=true; 
  33.                 echo $enword."的英文意思是:".getNodeVal($word_node,"en"); 
  34.                 echo "<br/><a href='wordView.php'>返回继续查询</a>"
  35.             } 
  36.         } 
  37.  
  38.         if(!$isEnter){ 
  39.             echo "无法查询"
  40.             echo "<br/><a href='wordView.php'>返回继续查询</a>"
  41.         } 
  42.     }else if($type=="添加"){ 
  43.         //接收 
  44.         $enword=$_REQUEST['enword']; 
  45.         $zhword=$_REQUEST['zhword']; 
  46.         if(!emptyempty($enword)&&!emptyempty($zhword)){     
  47.             //获取根节点 
  48.             $root=$doc->getElementsByTagName("words")->item(0); 
  49.              
  50.             $word=$doc->createElement("word"); 
  51.             $en=$doc->createElement("en",$enword); 
  52.             $zh=$doc->createElement("zh",$zhword); 
  53.  
  54.             //进行挂载 
  55.  
  56.             $root->appendChild($word); 
  57.             $word->appendChild($en); 
  58.             $word->appendChild($zh); 
  59.  
  60.             //保存xml文件 
  61.             $doc->save("words.xml"); 
  62.             echo "添加成功<br/><a href='wordView.php'>返回继续操作</a>"
  63.         }else
  64.              
  65.             echo "请输入单词"
  66.             echo "<br/><a href='wordView.php'>返回继续操作</a>"
  67.             exit(); 
  68.         } 
  69.     }else if($type=="delete"){ 
  70.          
  71.         $word=$_REQUEST['word']; 
  72.         //获取所有单词节点 
  73.         $words=$doc->getElementsByTagName("word"); 
  74.         $isEnter=false; 
  75.         //遍历单词节点 
  76.         for($i=0;$i<$words->length;$i++){ 
  77.             $word_node=$words->item($i); 
  78.             //获取不同的语种 
  79.             $en_word=getNodeVal($word_node,"en"); 
  80.             $zh_word=getNodeVal($word_node,"zh"); 
  81.             //查询 
  82.             if($word==$en_word || $word==$zh_word){ 
  83.                 $isEnter=true; 
  84.                 //找到父节点 
  85.                 $word_node->parentNode->removeChild($word_node); 
  86.                 $doc->save("words.xml"); 
  87.                 echo "删除成功<br/><a href='wordView.php'>返回继续操作</a>"
  88.             } 
  89.         } 
  90.  
  91.         if(!$isEnter){ 
  92.             echo "操作失败"
  93.             echo "<br/><a href='wordView.php'>返回继续操作</a>"
  94.         } 
  95.     }else if($type="修改"){ 
  96.         //接收 
  97.         $enword=$_REQUEST['enword']; 
  98.         $zhword=$_REQUEST['zhword']; 
  99.         if(!emptyempty($enword)&&!emptyempty($zhword)){     
  100.             //获取所有单词节点 
  101.             $words=$doc->getElementsByTagName("word"); 
  102.             //遍历单词节点 
  103.             $isEnter=false; 
  104.             for($i=0;$i<$words->length;$i++){ 
  105.                 $word_node=$words->item($i); 
  106.                 //获取不同的语种 
  107.                 $en_word=getNodeVal($word_node,"en"); 
  108.                 $zh_word=getNodeVal($word_node,"zh"); 
  109.                 //查询 
  110.                 if($enword==$en_word && $zhword!=$zh_word){ 
  111.                     //修改中文 
  112.                     $isEnter=true; 
  113.                     //获取zh节点 
  114.                     $zh=$word_node->getElementsByTagName("zh")->item(0); 
  115.                     $zh->nodeValue=$zhword
  116.                     $doc->save("words.xml"); 
  117.                     echo "修改成功"
  118.                     echo "<br/><a href='wordView.php'>返回继续操作</a>"
  119.                 }else if($enword!=$en_word && $zhword==$zh_word){ 
  120.                     //修改因为 
  121.                     $isEnter=true; 
  122.                     $en=$word_node->getElementsByTagName("en")->item(0); 
  123.                     $en->nodeValue=$enword
  124.                     $doc->save("words.xml"); 
  125.                     echo "修改成功"
  126.                     echo "<br/><a href='wordView.php'>返回继续操作</a>"
  127.                 }     
  128.             } 
  129.  
  130.             if(!$isEnter){ 
  131.                 echo "没有做任何修改"
  132.                 echo "<br/><a href='wordView.php'>返回继续操作</a>"
  133.             } 
  134.  
  135.         }else
  136.             echo "请输入需要修改的单词"
  137.             echo "<br/><a href='wordView.php'>返回继续操作</a>"
  138.             exit(); 
  139.         } 
  140.     } 
  141.     //开源代码phpfensi.com 
  142.     //获取节点的文本值 
  143.     function getNodeVal(&$MyNode,$tagName){ 
  144.         return $MyNode->getElementsByTagName($tagName)->item(0)->nodeValue; 
  145.     } 
  146. ?> 

words.xml,代码如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <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> 

Tags: PHP操作xml crud操作

分享到: