您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關php如何修改xml,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
php修改xml的方法:首先創建一個代碼示例文件;然后通過“$new->nodeValue=$_content;”方法修改節點值即可。
php創建、增加、刪除、修改xml
創建xml 方法:
<?php $xmlpatch = 'index.xml'; $_id = '1'; $_title = 'title1'; $_content = 'content1'; $_author = 'author1'; $_sendtime = 'time1'; $_htmlpatch = '1.html'; $doc = new DOMDocument('1.0', 'utf-8'); $doc -> formatOutput = true; $root = $doc -> createElement('root');//新建節點 $index = $doc -> createElement('index');//新建節點 $url = $doc -> createAttribute('url');//新建屬性 $patch = $doc -> createTextNode($_htmlpatch);//新建TEXT值 $url -> appendChild($patch);//將$patch文本設為$url屬性的值 $id = $doc -> createAttribute('id'); $newsid = $doc -> createTextNode($_id); $id -> appendChild($newsid); $title = $doc -> createAttribute('title'); $newstitle = $doc -> createTextNode($_title); $title -> appendChild($newstitle); $content = $doc -> createTextNode($_content);//節點值 $author = $doc -> createAttribute('author'); $newsauthor = $doc -> createTextNode($_author); $author -> appendChild($newsauthor); $sendtime = $doc -> createAttribute('time'); $newssendtime = $doc -> createTextNode($_sendtime); $sendtime -> appendChild($newssendtime); $index -> appendChild($id);//將$id設為index節點的屬性,以下類同 $index -> appendChild($title); $index -> appendChild($content); $index -> appendChild($url); $index -> appendChild($author); $index -> appendChild($sendtime); $root -> appendChild($index);//設置index為root字節點 $doc -> appendChild($root);//設置root為跟節點 $doc -> save($xmlpatch);//保存文件 echo $xmlpatch . ' has create success'; ?>
增加xml 的節點
<?php $xmlpatch = 'index.xml'; $_id = '3'; $_title = 'title3'; $_content = 'content3'; $_author = 'author3'; $_sendtime = 'time3'; $_htmlpatch = '3.html'; $doc = new DOMDocument(); $doc -> formatOutput = true; if($doc -> load($xmlpatch)) { $root = $doc -> documentElement;//獲得根節點(root) $index = $doc -> createElement('index'); $url = $doc -> createAttribute('url'); $patch = $doc -> createTextNode($_htmlpatch); $url -> appendChild($patch); $id = $doc -> createAttribute('id'); $newsid = $doc -> createTextNode($_id); $id -> appendChild($newsid); $title = $doc -> createAttribute('title'); $newstitle = $doc -> createTextNode($_title); $title -> appendChild($newstitle); $content = $doc -> createTextNode($_content); $author = $doc -> createAttribute('author'); $newsauthor = $doc -> createTextNode($_author); $author -> appendChild($newsauthor); $sendtime = $doc -> createAttribute('time'); $newssendtime = $doc -> createTextNode($_sendtime); $sendtime -> appendChild($newssendtime); $index -> appendChild($id); $index -> appendChild($title); $index -> appendChild($content); $index -> appendChild($url); $index -> appendChild($author); $index -> appendChild($sendtime); $root -> appendChild($index); $doc -> save($xmlpatch); echo $_id . ' has been added in ' . $xmlpatch; } else { echo 'xml file loaded error!'; } ?>
刪除xml 的節點
<?php $xmlpatch = 'index.xml'; $_id = '3'; $doc = new DOMDocument(); $doc -> formatOutput = true; if($doc -> load($xmlpatch)) { $root = $doc -> documentElement; $elm = $root -> getElementsByTagName('index'); foreach ($elm as $new) { if($new -> getAttribute('id') == $_id) { if($root -> removeChild($new)) { echo $_id . ' has been deleted'; } else { echo $_id . ' delete failed'; } } } $doc -> save($xmlpatch); } else { echo 'xml file loaded error!'; } ?>
修改XML的節點
<?php $xmlpatch = 'index.xml'; $_id = '2'; $_title = 'has been changed'; $_content = 'has been changed'; $doc = new DOMDocument(); $doc -> formatOutput = true; if($doc -> load($xmlpatch)) { $root = $doc -> documentElement; $elm = $root -> getElementsByTagName('index'); $checkexist = 0; foreach ($elm as $new) { if($new -> getAttribute('id') == $_id) { $new -> setAttribute('title', $_title); $new -> nodeValue = $_content;//修改節點值,真是太意外了,沒想到跟JS一樣直接能賦值... //$new -> removeChild($new -> nodevalue); $checkexist = 1; } } if($checkexist == 0) { echo $_id . ' is not found in ' . $xmlpatch; } else { $doc -> save($xmlpatch); echo $_id . ' has been changed'; } } else { echo 'xml file loaded error!'; } ?>
關于php如何修改xml就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。