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

作者:袖梨 2022-06-24

html代码页面

 代码如下 复制代码



   


   

wordProcess.php" method="post">
       
       
       
   

    添加单词
   

        英文:

        中文:

       
       
       
   

   

       
       
       
   


wordpress.php文件

 代码如下 复制代码


    //接收类型 看看用户做什么(查询、添加....)
    $type=$_REQUEST['type'];
    //echo $type;
    //exit();
    //创建xml文档对象
    $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 "
返回继续查询";
            }else if($enword==$zh_word){
                $isEnter=true;
                echo $enword."的英文意思是:".getNodeVal($word_node,"en");
                echo "
返回继续查询";
            }
        }

        if(!$isEnter){
            echo "无法查询";
            echo "
返回继续查询";
        }
    }else if($type=="添加"){
        //接收
        $enword=$_REQUEST['enword'];
        $zhword=$_REQUEST['zhword'];
        if(!empty($enword)&&!empty($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 "添加成功
返回继续操作";
        }else{
           
            echo "请输入单词";
            echo "
返回继续操作";
            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 "删除成功
返回继续操作";
            }
        }

        if(!$isEnter){
            echo "操作失败";
            echo "
返回继续操作";
        }
    }else if($type="修改"){
        //接收
        $enword=$_REQUEST['enword'];
        $zhword=$_REQUEST['zhword'];
        if(!empty($enword)&&!empty($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 "
返回继续操作";
                }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 "
返回继续操作";
                }   
            }

            if(!$isEnter){
                echo "没有做任何修改";
                echo "
返回继续操作";
            }

        }else{
            echo "请输入需要修改的单词";
            echo "
返回继续操作";
            exit();
        }
    }
   
    //获取节点的文本值
    function getNodeVal(&$MyNode,$tagName){
        return $MyNode->getElementsByTagName($tagName)->item(0)->nodeValue;
    }
?>

words.xml

 代码如下 复制代码


boy男孩girl女孩fireword词库

相关文章

精彩推荐