您好,登錄后才能下訂單哦!
這篇文章主要介紹了PHP如何讀取XML文件,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
php的框架:1、Laravel,Laravel是一款免費(fèi)并且開源的PHP應(yīng)用框架。2、Phalcon,Phalcon是運(yùn)行速度最快的一個(gè)PHP框架。3、Symfony,Symfony是一款為Web項(xiàng)目準(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)建一個(gè)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;
實(shí)例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);
實(shí)例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
實(shí)例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);
實(shí)例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文件”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。