php 創(chuàng)建、刪除、增加、修改xml

參考http://blog.csdn.net/k8080880/article/details/47616461
創(chuàng)建

<?php
    $file = 'index.xml'; 
    $idValue = "1"; 
    $urlValue = 'http://www.baidu.com';  
    $contentValue = "百度"; 

    $newIdValue = "2";
    $newUrlValue = "http://google.cn";
    $newContentValue = "谷歌";

    $doc = new DOMDocument('1.0', 'utf-8');  
    $doc -> formatOutput = true;

    $root = $doc -> createElement('root');//新建節(jié)點(diǎn)  
    $test = $doc -> createElement('test');//新建節(jié)點(diǎn)  

    $url = $doc -> createAttribute('url');//新建屬性  
    $urlValueNode = $doc -> createTextNode($urlValue);//新建TEXT值  
    $url -> appendChild($urlValueNode);//將$patch文本設(shè)為$url屬性的值  
        
    $id = $doc -> createAttribute('id');   
    $idValueNode = $doc -> createTextNode($idValue);   
    $id -> appendChild($idValueNode);

    $content = $doc -> createTextNode($contentValue);//節(jié)點(diǎn)值    
       
    $test -> appendChild($url);//將$url設(shè)為index節(jié)點(diǎn)的屬性,以下類同  
    $test -> appendChild($id);
    $test -> appendChild($content); 
    $root -> appendChild($test);//設(shè)置index為root子節(jié)點(diǎn)  
    $doc -> appendChild($root);//設(shè)置root為跟節(jié)點(diǎn) 

    $doc -> save($file);//保存文件  
       
    echo '創(chuàng)建成功'."\n"; 
?>

增加

<?php
    $file = 'index.xml'; 
    $idValue = "1"; 
    $urlValue = 'http://www.baidu.com';  
    $contentValue = "百度"; 

    $newIdValue = "2";
    $newUrlValue = "http://google.cn";
    $newContentValue = "谷歌";

    $doc = new DOMDocument('1.0', 'utf-8');  
    $doc -> formatOutput = true;

    if($doc->load($file)) {
        $root = $doc -> documentElement;//獲得根節(jié)點(diǎn)(root)  

        $test = $doc -> createElement('test');

        $url = $doc -> createAttribute('url');
        $newUrlValueNode = $doc -> createTextNode($newUrlValue);   
        $url -> appendChild($newUrlValueNode);

        $id = $doc -> createAttribute('id');   
        $newIdValueNode = $doc -> createTextNode($newIdValue);   
        $id -> appendChild($newIdValueNode);

        $content = $doc -> createTextNode($newContentValue);//節(jié)點(diǎn)值  

        $test -> appendChild($url);
        $test -> appendChild($id);
        $test -> appendChild($content);
        $root -> appendChild($test);
        $doc -> save($file); 

        echo "添加成功"."\n"; 
    }
?>

修改

<?php
    $file = 'index.xml'; 
    $idValue = "1"; 
    $urlValue = 'http://www.baidu.com';  
    $contentValue = "百度"; 

    $newIdValue = "2";
    $newUrlValue = "http://google.cn";
    $newContentValue = "谷歌";

    $doc = new DOMDocument('1.0', 'utf-8');  
    $doc -> formatOutput = true;

    if($doc->load($file)) {
        $isExist = false;
        $root = $doc -> documentElement;//獲得根節(jié)點(diǎn)(root)  
        $elm = $root -> getElementsByTagName('test');
        foreach ($elm as $item) {   
            if($item -> getAttribute('id') == $idValue) {   
                $item -> setAttribute('url', $newUrlValue);   
                $item -> nodeValue = $newContentValue;//修改節(jié)點(diǎn)值

                $title = $doc -> createAttribute('title');//新建屬性  
                $titleValueNode = $doc -> createTextNode("標(biāo)題");//新建TEXT值  
                $title -> appendChild($titleValueNode);//將$patch文本設(shè)為$url屬性的值 

                $item -> appendChild($title);

                $isExist = true;   
            }   
        }   
        if(!$isExist) {   
            echo "沒有找到節(jié)點(diǎn)";   
        } else {   
            $doc -> save($file);   
            echo "修改成功";     
        }
    }
?>

刪除

<?php
    $file = 'index.xml'; 
    $idValue = "1"; 
    $urlValue = 'http://www.baidu.com';  
    $contentValue = "百度"; 

    $newIdValue = "2";
    $newUrlValue = "http://google.cn";
    $newContentValue = "谷歌";

    $doc = new DOMDocument('1.0', 'utf-8');  
    $doc -> formatOutput = true;

    if($doc->load($file)) {
        $root = $doc -> documentElement;//獲得根節(jié)點(diǎn)(root)  
        $elm = $root -> getElementsByTagName('test');
        foreach ($elm as $item) {   
            if($item -> getAttribute('id') == $newIdValue) { 
                if($root -> removeChild($item)) { 
                    echo "刪除成功";
                } else {   
                    echo '刪除失敗';   
                } 
                $item -> setAttribute('url', $idValue);   
                $item -> nodeValue = $contentValue;//修改節(jié)點(diǎn)值
                //$item -> removeChild($item -> nodevalue); 
            }   
        }   
    
        $doc -> save($file); 
    }
?>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容