新聞中心
這篇文章將為大家詳細(xì)講解有關(guān)php如何修改xml,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)公司是一家專(zhuān)注于成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司與策劃設(shè)計(jì),延津網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:延津等地區(qū)。延津做網(wǎng)站價(jià)格咨詢(xún):18982081108
php修改xml的方法:首先創(chuàng)建一個(gè)代碼示例文件;然后通過(guò)“$new->nodeValue=$_content;”方法修改節(jié)點(diǎn)值即可。
php創(chuàng)建、增加、刪除、修改xml
創(chuàng)建xml 方法:
formatOutput = true; $root = $doc -> createElement('root');//新建節(jié)點(diǎn) $index = $doc -> createElement('index');//新建節(jié)點(diǎn) $url = $doc -> createAttribute('url');//新建屬性 $patch = $doc -> createTextNode($_htmlpatch);//新建TEXT值 $url -> appendChild($patch);//將$patch文本設(shè)為$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);//節(jié)點(diǎn)值 $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設(shè)為index節(jié)點(diǎn)的屬性,以下類(lèi)同 $index -> appendChild($title); $index -> appendChild($content); $index -> appendChild($url); $index -> appendChild($author); $index -> appendChild($sendtime); $root -> appendChild($index);//設(shè)置index為root字節(jié)點(diǎn) $doc -> appendChild($root);//設(shè)置root為跟節(jié)點(diǎn) $doc -> save($xmlpatch);//保存文件 echo $xmlpatch . ' has create success'; ?>
增加xml 的節(jié)點(diǎn)
formatOutput = true; if($doc -> load($xmlpatch)) { $root = $doc -> documentElement;//獲得根節(jié)點(diǎn)(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 的節(jié)點(diǎn)
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的節(jié)點(diǎn)
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;//修改節(jié)點(diǎn)值,真是太意外了,沒(méi)想到跟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!'; } ?>
關(guān)于php如何修改xml就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁(yè)題目:php如何修改xml
鏈接URL:http://www.dlmjj.cn/article/ghpogi.html