php 修改、增加xml結(jié)點(diǎn)屬性的實(shí)現(xiàn)代碼
php 修改 增加xml結(jié)點(diǎn)屬性的代碼,供大家學(xué)習(xí)參考。
php修改xml結(jié)點(diǎn)屬性,增加xml結(jié)點(diǎn)屬性的代碼,有需要的朋友,參考下。
1、xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<clientSet>
<server url="192.168.0.180" port="1935" />
<rootPath value="" />
<homePath value="http://www.aaa.com" />
<helpPath value="help.html" />
<language value="en" />
<theme value="default" />
<visibleMarquee value = "true" />
<visibleWhitePaper value="true" />
<showMemberRoomForGuest value = "true" />
<emotions enabled="true" column="5" autoPlay="false">
<item name="Birthday" src="cartoon/movie/birthday.swf" thumb="cartoon/preview/birthday-small.swf" duration="15"/>
<item name="Boom" src="cartoon/movie/boom.swf" thumb="cartoon/preview/boom-small.swf" duration="6"/>
<item name="Bubble" src="cartoon/movie/bubble.swf" thumb="cartoon/preview/bubble-small.swf" duration="7.5"/>
<item name="Cry" src="cartoon/movie/cry.swf" thumb="cartoon/preview/cry-small.swf" duration="5.4"/>
<item name="Doggie" src="cartoon/movie/doggie.swf" thumb="cartoon/preview/doggie-small.swf" duration="13"/>
<item name="Greeting" src="cartoon/movie/greeting.swf" thumb="cartoon/preview/greeting-small.swf" duration="7.4"/>
<item name="Football" src="cartoon/movie/football.swf" thumb="cartoon/preview/football-small.swf" duration="2.2"/>
</emotions >
</clientSet>
2、php代碼
<?
$dom=new DOMDocument('1.0');
$dom->load('x.xml');
$em=$dom->getElementsByTagName('emotions');
$em=$em->item(0);
$items=$em->getElementsByTagName('item');
foreach($items as $a){
foreach($a->attributes as $b){
if($b->nodeValue=='Birthday'){
$a->setAttribute('name','nBirthday');
}
}
}
$t=$dom->createElement('item');
$t->setAttribute('name','x');
$t->setAttribute('src','www.baidu.com');
$t->setAttribute('duration','duration');
$em->appendChild($t);
$dom->save('x.xml');
?>
PHP解析XML文檔屬性并編輯
<?php
//讀取xml
$dom=new DOMDocument('1.0');
$dom->load('data.xml');
$em=$dom->getElementsByTagName('videos');//最外層節(jié)點(diǎn)
$em=$em->item(0);
$items=$em->getElementsByTagName('video');//節(jié)點(diǎn)
//如果不用讀取直接添加的話把下面這一段去掉即可
foreach($items as $a){
foreach($a->attributes as $b){//$b->nodeValue;節(jié)點(diǎn)屬性的值$b->nodeName;節(jié)點(diǎn)屬性的名稱
echo $b->nodeName;
echo ":";
echo $b->nodeValue;
echo "<br/>";
}
}
//下面是往xml寫(xiě)入一行新的
$t=$dom->createElement('video');//<video
$t->setAttribute('title','1');//<video name="data"
$t->setAttribute('src','2');//<video name="data" src="2"
$t->setAttribute('img','1');//<video name="data" img="1"
$em->appendChild($t);//<video name="data" img="1"/>
$dom->save('data.xml');
?>
當(dāng)時(shí)的xml文檔:
<?xml version="1.0"?>
<videos>
<video img="a" url="1" title="1" nickname="1" tag="1" vid="1" star="1"/>
<video img="b" url="2" title="2" nickname="2" tag="2" vid="2" star="2"/>
<video img="c" url="3" title="3" nickname="3" tag="3" vid="3" star="3"/>
<video title="d" src="2" img="1"/>
</videos>
//下面這一個(gè)文件是后改的可以修改xml
<?php
$doc = new DOMDocument();
$doc->load('data.xml');
//查找 videos 節(jié)點(diǎn)
$root = $doc->getElementsByTagName('videos');
//第一個(gè) videos 節(jié)點(diǎn)
$root = $root->item(0);
//查找 videos 節(jié)點(diǎn)下的 video 節(jié)點(diǎn)
$userid = $root->getElementsByTagName('video');
//遍歷所有 video 節(jié)點(diǎn)
foreach ($userid as $rootdata)
{
//遍歷每一個(gè) video 節(jié)點(diǎn)所有屬性
foreach ($rootdata->attributes as $attrib)
{
$attribName = $attrib->nodeName; //nodeName為屬性名稱
$attribValue = $attrib->nodeValue; //nodeValue為屬性內(nèi)容
//查找屬性名稱為ip的節(jié)點(diǎn)內(nèi)容
if ($attribName =='img')
{
//查找屬性內(nèi)容為ip的節(jié)點(diǎn)內(nèi)容
if ($attribValue =='1')
{
//將屬性為img,img內(nèi)容為1的修改為image;
$rootdata->setAttribute('img','image');
$doc->save('data.xml');
}
}
}
}
?>
- php對(duì)xml文件的增刪改查操作實(shí)現(xiàn)方法分析
- PHP實(shí)現(xiàn)對(duì)xml進(jìn)行簡(jiǎn)單的增刪改查(CRUD)操作示例
- PHP對(duì)XML內(nèi)容進(jìn)行修改和刪除實(shí)例代碼
- php中DOMDocument簡(jiǎn)單用法示例代碼(XML創(chuàng)建、添加、刪除、修改)
- PHP中開(kāi)發(fā)XML應(yīng)用程序之基礎(chǔ)篇 添加節(jié)點(diǎn) 刪除節(jié)點(diǎn) 查詢節(jié)點(diǎn) 查詢節(jié)
- 通過(guò)php修改xml文檔內(nèi)容的方法
- 自定義php類(查找/修改)xml文檔
- php XPath對(duì)XML文件查找及修改實(shí)現(xiàn)代碼
- PHP中使用SimpleXML檢查XML文件結(jié)構(gòu)實(shí)例
- PHP實(shí)現(xiàn)對(duì)xml的增刪改查操作案例分析
相關(guān)文章
PHP內(nèi)置過(guò)濾器FILTER使用實(shí)例
這篇文章主要介紹了PHP內(nèi)置過(guò)濾器FILTER使用實(shí)例,列舉了驗(yàn)證功能和糾錯(cuò)功能的代碼例子來(lái)講解如何使用FILTER,需要的朋友可以參考下2014-06-06php實(shí)現(xiàn)帶讀寫(xiě)分離功能的MySQL類完整實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)帶讀寫(xiě)分離功能的MySQL類,具有針對(duì)mysql數(shù)據(jù)庫(kù)的基本增刪改查及讀寫(xiě)分離操作的功能,涉及針對(duì)讀庫(kù)與寫(xiě)庫(kù)的判斷及操作技巧,需要的朋友可以參考下2016-07-07php實(shí)現(xiàn)簡(jiǎn)易聊天室應(yīng)用代碼
聊天應(yīng)用程序在網(wǎng)上非常常見(jiàn),開(kāi)發(fā)人員在構(gòu)建這類應(yīng)用程序時(shí)的選擇也很多。這篇文章介紹了如何實(shí)現(xiàn)基于PHP-AJAX的聊天應(yīng)用程序,并且不需要刷新頁(yè)面就可以發(fā)送和接收消息,需要的朋友可以參考下2015-09-09PHP ADODB實(shí)現(xiàn)分頁(yè)功能簡(jiǎn)單示例
這篇文章主要介紹了PHP ADODB實(shí)現(xiàn)分頁(yè)功能,結(jié)合實(shí)例形式分析了php使用ADODB實(shí)現(xiàn)分頁(yè)功能相關(guān)操作技巧,需要的朋友可以參考下2018-05-05php常用數(shù)組array函數(shù)實(shí)例總結(jié)【賦值,拆分,合并,計(jì)算,添加,刪除,查詢,判斷,排序】
這篇文章主要介紹了php常用數(shù)組array函數(shù),結(jié)合實(shí)例形式總結(jié)分析了php常用的數(shù)組操作函數(shù),包括數(shù)組的賦值、拆分、合并、計(jì)算、添加、刪除、查詢、判斷、排序等,需要的朋友可以參考下2016-12-12解決wincache不支持64位PHP5.5/5.6的問(wèn)題(提供64位wincache下載)
這篇文章主要解決wincache不支持64位PHP5.5/5.6的問(wèn)題,并提供64位wincache的下載,需要的朋友可以參考下。2016-06-06