你好,PHP操作XML新人為你解答!

創(chuàng)新互聯(lián)公司主營(yíng)西城網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,手機(jī)APP定制開發(fā),西城h5成都小程序開發(fā)搭建,西城網(wǎng)站營(yíng)銷推廣歡迎西城等地區(qū)企業(yè)咨詢
$xml?=?simplexml_load_file($xmlsrc);//初始化并獲取其根
//看得出來,你的user要插入到根的下一層,那么就不必進(jìn)行循環(huán)索引了,因?yàn)楦呀?jīng)獲取到了
$user?=?$xml-addChild("user",?"");//在$xml節(jié)點(diǎn)下插入名為user的節(jié)點(diǎn),且必須賦值,這里因?yàn)闆]有值,所以就留空,但不能不寫。
//然后再在user節(jié)點(diǎn)下插入節(jié)點(diǎn),這里已經(jīng)把user節(jié)點(diǎn)賦給$user了
$name?=?$user-addChild("name",?$_POST["name"]);//把接收到的name值添加到user節(jié)點(diǎn)下的name節(jié)點(diǎn)中,下同
$sex?=?$user-addChild("sex",?$_POST["sex"]);//因?yàn)檫@里的name和sex添加后不再用了,可以不把節(jié)點(diǎn)賦值出來,即不須寫上$sex=
$hei?=?$user-addChild("height",?$_POST["height"]);
$hei-addAttribute("type",?"cm");//給height節(jié)點(diǎn)添加屬性
這個(gè)SimpleXML我昨天才開始學(xué),感覺挺不錯(cuò)的樣子,操作代碼直觀,可讀性強(qiáng),多用用熟練了就好
我知道一個(gè)比較笨的方法 利用php控制文件的方式 將整個(gè)xml文件讀入一個(gè)變量, 然后用str_replace(正則表達(dá)式) 去匹配拆分成數(shù)組 最后 連接數(shù)組 連接時(shí) 加入你要新增的元素
或者
用php 內(nèi)置的xml 語法解析函數(shù) 通常不太好用 可以去找一些國(guó)外的 xml 解析器的二次封裝
php入門者飄過~~~
1.xml: ?
?xml?version="1.0"?encoding="utf-8"?
vip
user
email/email
time2016-05-31/time
ip127.0.0.1/ip
/user
/vip
php:
?php?
$xmlstr?=?file_get_contents('1.xml');
$users?=?new?SimpleXMLElement($xmlstr);
$user?=?$users-addChild('user');
$user-addChild('email',?'test');
$user-addChild('time',?'test');
$user-addChild('ip',?'127.0.0.1');
$users-asXML();
file_put_contents('1.xml',?$users-asXML());
應(yīng)該是這樣:
$doc = new DOMDocument();
$doc-load( "pic.xml");
//這里你要獲取他的第一個(gè)tag,因?yàn)間etElementsByTagName獲取的結(jié)果是個(gè)數(shù)組對(duì)象
$folder = $doc-getElementsByTagName("folder") - item(0);
$new_file=$doc-createElement("file");
$pname = $doc - createAttribute('pname');
$pclass = $doc - createAttribute('class');
$pcar = $doc - createAttribute('pcar');
$newspname = $doc - createTextNode($name);
$newspclass = $doc - createTextNode($_POST['Sclass']);
$newspcar = $doc - createTextNode($_POST['Scar']);
$pname - appendChild($newspname);
$pclass - appendChild($newspclass);
$pcar - appendChild($newspcar);
$new_file - appendChild($pname);
$new_file - appendChild($pclass);
$new_file - appendChild($pcar);
$folder - appendChild($new_file);
//$doc -appendChild($folder);//前面已經(jīng)append了,就不需要在append了
$doc - save("pic.xml");
最后第二句添加有錯(cuò) 應(yīng)該這樣寫:
$a=$doc-createElement('a'); //定義一個(gè)a標(biāo)簽
$a_text=$doc-createTextNode("0000000000"); //給a標(biāo)簽一個(gè)值
$a-appendChild($a_text); //把值加載到a標(biāo)簽
$id=$doc-createAttribute('id'); //定義一個(gè)id屬性
$id_text=$doc-createTextNode("xxx"); //給id屬性一個(gè)值
$id-appendChild($id_text); //把值加載到id屬性中
$a-appendChild($id); //把id屬性加載到a標(biāo)簽中
$php100-appendChild($a); //把a(bǔ)標(biāo)簽加載到php100標(biāo)簽中
本文題目:php向xml添加數(shù)據(jù),php解析xml數(shù)據(jù)
網(wǎng)站URL:http://chinadenli.net/article47/dsihcej.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站收錄、關(guān)鍵詞優(yōu)化、網(wǎng)站改版、ChatGPT、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)