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

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

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>