PHP讀取XML文件的方法實(shí)例總結(jié)【DOMDocument及simplexml方法】
本文實(shí)例講述了PHP讀取XML文件的方法。分享給大家供大家參考,具體如下:
使用DOMDocument對(duì)象讀取xml
創(chuàng)建一個(gè)DOMDocument對(duì)象
$doc = new DOMDocument();
載入xml文件
$doc->load("book.xml");
獲取標(biāo)簽對(duì)象
$books = $doc->getElementsByTagName("book");
獲取標(biāo)簽的子對(duì)象
$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對(duì)象 $doc->load("book.xml"); //打開book.xml $books = $doc->getElementsByTagName("book"); //獲取book標(biāo)簽對(duì)象 foreach ($books as $book){ //遍歷對(duì)象 $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對(duì)象 $doc->load("config.xml"); //打開config.xml $mysql = $doc->getElementsByTagName("mysql"); //獲取mysql標(biāo)簽對(duì)象 $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);
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP針對(duì)XML文件操作技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP使用DOMDocument類生成HTML實(shí)例(包含常見標(biāo)簽元素)
- 如何解決php domdocument找不到的問題
- PHP中使用DOMDocument來(lái)處理HTML、XML文檔的示例
- PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】
- PHP基于DOMDocument解析和生成xml的方法分析
- PHP 中 DOMDocument保存xml時(shí)中文出現(xiàn)亂碼問題的解決方案
- php中DOMDocument簡(jiǎn)單用法示例代碼(XML創(chuàng)建、添加、刪除、修改)
- PHP XML操作類DOMDocument
- php基于DOMDocument操作頁(yè)面元素實(shí)例
相關(guān)文章
Linux fgetcsv取得的數(shù)組元素為空字符串的解決方法
使用CSV導(dǎo)入數(shù)據(jù)的時(shí)候,我們通常用的是Windows系統(tǒng),使用GBK在Windows的Excel中編輯2011-11-11php 動(dòng)態(tài)執(zhí)行帶有參數(shù)的類方法
PHP中,在事先知道類和類的方法名稱,使用call_user_func函數(shù)可以做動(dòng)態(tài)執(zhí)行。2009-04-04php中實(shí)現(xiàn)字符串翻轉(zhuǎn)的方法
本文主要介紹了實(shí)現(xiàn)php字符串翻轉(zhuǎn)的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02windows下zendframework項(xiàng)目環(huán)境搭建(通過(guò)命令行配置)
本文將詳細(xì)介紹windows下通過(guò)命令行配置zendframework項(xiàng)目環(huán)境,需要了解的朋友可以參考下2012-12-12php下把數(shù)組保存為文件格式的實(shí)例應(yīng)用
我們通常把一些常用的數(shù)據(jù)保存為數(shù)組格式方便調(diào)用,同時(shí)這也是緩存的重要方法。2010-02-02