這篇文章主要介紹了PHP如何讀取XML文件,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

php的框架:1、Laravel,Laravel是一款免費并且開源的PHP應(yīng)用框架。2、Phalcon,Phalcon是運(yùn)行速度最快的一個PHP框架。3、Symfony,Symfony是一款為Web項目準(zhǔn)備的PHP框架。4、Yii,Yii是一款快速、安全和專業(yè)的PHP框架。5、CodeIgniter,CodeIgniter是一款非常敏捷的開源PHP框架。6、CakePHP,CakePHP是一款老牌的PHP框架。7.Kohana,Kohana是一款敏捷但是功能強(qiáng)大的PHP框架。
具體如下:
使用DOMDocument對象讀取xml
創(chuàng)建一個DOMDocument對象
$doc = new DOMDocument();
載入xml文件
$doc->load("book.xml");獲取標(biāo)簽對象
$books = $doc->getElementsByTagName("book");獲取標(biāo)簽的子對象
$titles = $book->getElementsByTagName("title");獲取標(biāo)簽的值或?qū)傩?/p>
$title = $titles->item(0)->nodeValue;
實例1,獲取圖書列表
book.xml
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book> <title>PHP和MySQL開發(fā)</title> <author>譚浩強(qiáng)</author> </book> <book> <titile>xml從入門到精通</titile> <author>鄭智化</author> </book> </bookstore>
load.php
<?php
header("Content-type:text/html;charset=utf8");
$doc = new DOMDocument(); //創(chuàng)建DOMDocument對象
$doc->load("book.xml"); //打開book.xml
$books = $doc->getElementsByTagName("book"); //獲取book標(biāo)簽對象
foreach ($books as $book){ //遍歷對象
$titles = $book->getElementsByTagName("title"); //獲取book標(biāo)簽下的title標(biāo)簽
$title = $titles->item(0)->nodeValue; //獲取標(biāo)簽的值
$authors = $book->getElementsByTagName("author");//獲取book標(biāo)簽下的author標(biāo)簽
$author = $authors->item(0)->nodeValue; //獲取標(biāo)簽的值
$item["title"] = $title;
$item["author"] = $author;
$bookinfo[] = $item;
}
var_dump($bookinfo);實例2,讀取配置文件
config.xml
<?xml version="1.0" encoding="UTF-8"?> <mysql> <host>127.0.0.1</host> <username>root</username> <password></password> <database>test</database> </mysql>
config.php
<?php
header("Content-type:text/html;charset=utf8");
$doc = new DOMDocument(); //創(chuàng)建DOMDocument對象
$doc->load("config.xml"); //打開config.xml
$mysql = $doc->getElementsByTagName("mysql"); //獲取mysql標(biāo)簽對象
$host = $mysql->item(0)->getElementsByTagName("host");
$config["host"] = $host->item(0)->nodeValue;
$username = $mysql->item(0)->getElementsByTagName("username");
$config["username"] = $username->item(0)->nodeValue;
$password = $mysql->item(0)->getElementsByTagName("password");
$config["password"] = $password->item(0)->nodeValue;
$database = $mysql->item(0)->getElementsByTagName("database");
$config["database"] = $database->item(0)->nodeValue;
var_dump($config);使用simplexml方法讀取xml
實例1,獲取圖書列表
load.php
<?php
header("Content-type:text/html;charset=utf8");
$books = simplexml_load_file("book.xml");
foreach($books as $book){
$item["title"] = $book->title;
$item["author"] = $book->author;
$booklist[] = $item;
}
var_dump($booklist);實例2,讀取配置文件
config.php
<?php
header("Content-type:text/html;charset=utf8");
$mysql = simplexml_load_file("config.xml");
$config['host'] = $mysql->host;
$config['username'] = $mysql->username;
$config['password'] = $mysql->password;
$config['databse'] = $mysql->database;
var_dump($config);感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“PHP如何讀取XML文件”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
分享標(biāo)題:PHP如何讀取XML文件-創(chuàng)新互聯(lián)
URL標(biāo)題:http://chinadenli.net/article16/gcgdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、微信小程序、網(wǎng)站導(dǎo)航、手機(jī)網(wǎng)站建設(shè)、定制網(wǎng)站、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容