文章詳情頁(yè)
php讀取xml中某個(gè)元素的內(nèi)容(PHP5以上才支持)
瀏覽:33日期:2024-01-08 14:09:22
假設(shè)xml文件名為class.xml,內(nèi)容如下:
<?xml version=”1.0″ encoding=”utf-8″?><class> <student> <id>3</id> <name>張三</name> </student> <student> <id>5</id> <name>李四</name> </student></class>
1、載入xml文件并生成一個(gè)simpleXml對(duì)象
//假設(shè)xml文件在當(dāng)前路徑
$xml = simplexml_load_file(’class.xml’);
2、根據(jù)元素的名字,一層一層找到那個(gè)元素,并獲取元素內(nèi)容
//假設(shè)獲取的是第二個(gè)student中的子元素中,第一個(gè)name的內(nèi)容,也就是輸出李四。注意元素的索引從0開始
echo $xml->student[1]->name[0];
標(biāo)簽:
PHP
排行榜
