Home php教程 php手册 PHP 操作xml编程之xml的crud操作

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

May 25, 2016 pm 04:42 PM
crud operation

本文章来给大家介绍一个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> 
Copy after login

wordpress.php文件,代码如下:

<?php 
	 
	    //接收类型 看看用户做什么(查询、添加....) 
	    $type=$_REQUEST[&#39;type&#39;]; 
	    //echo $type; 
	    //exit(); 
	    //创建xml文档对象 
	    $doc=new DOMDocument(); 
	    $doc->load("words.xml"); 
	 
	    //进行判断 
	    if($type=="query"){ 
	        //获取用户输入的值 
	        $enword=$_REQUEST[&#39;enword&#39;]; 
	         
	        //判断是否进入查询 
	        $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=&#39;wordView.php&#39;>返回继续查询</a>"; 
	            }else if($enword==$zh_word){ 
	                $isEnter=true; 
	                echo $enword."的英文意思是:".getNodeVal($word_node,"en"); 
	                echo "<br/><a href=&#39;wordView.php&#39;>返回继续查询</a>"; 
	            } 
	        } 
	 
	        if(!$isEnter){ 
	            echo "无法查询"; 
	            echo "<br/><a href=&#39;wordView.php&#39;>返回继续查询</a>"; 
	        } 
	    }else if($type=="添加"){ 
	        //接收 
	        $enword=$_REQUEST[&#39;enword&#39;]; 
	        $zhword=$_REQUEST[&#39;zhword&#39;]; 
	        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); 
	 
	            //保存xml文件 
	            $doc->save("words.xml"); 
	            echo "添加成功<br/><a href=&#39;wordView.php&#39;>返回继续操作</a>"; 
	        }else{ 
	             
	            echo "请输入单词"; 
	            echo "<br/><a href=&#39;wordView.php&#39;>返回继续操作</a>"; 
	            exit(); 
	        } 
	    }else if($type=="delete"){ 
	         
	        $word=$_REQUEST[&#39;word&#39;]; 
	        //获取所有单词节点 
	        $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=&#39;wordView.php&#39;>返回继续操作</a>"; 
	            } 
	        } 
	 
	        if(!$isEnter){ 
	            echo "操作失败"; 
	            echo "<br/><a href=&#39;wordView.php&#39;>返回继续操作</a>"; 
	        } 
	    }else if($type="修改"){ 
	        //接收 
	        $enword=$_REQUEST[&#39;enword&#39;]; 
	        $zhword=$_REQUEST[&#39;zhword&#39;]; 
	        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节点 
	                    $zh=$word_node->getElementsByTagName("zh")->item(0); 
	                    $zh->nodeValue=$zhword; 
	                    $doc->save("words.xml"); 
	                    echo "修改成功"; 
	                    echo "<br/><a href=&#39;wordView.php&#39;>返回继续操作</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=&#39;wordView.php&#39;>返回继续操作</a>"; 
	                }     
	            } 
	 
	            if(!$isEnter){ 
	                echo "没有做任何修改"; 
	                echo "<br/><a href=&#39;wordView.php&#39;>返回继续操作</a>"; 
	            } 
	 
	        }else{ 
	            echo "请输入需要修改的单词"; 
	            echo "<br/><a href=&#39;wordView.php&#39;>返回继续操作</a>"; 
	            exit(); 
	        } 
	    } 
	    //开源代码phprm.com 
	    //获取节点的文本值 
	    function getNodeVal(&$MyNode,$tagName){ 
	        return $MyNode->getElementsByTagName($tagName)->item(0)->nodeValue; 
	    } 
	 
Copy after login

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> 
Copy after login

永久地址:

转载随意~请带上教程地址吧^^

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)