婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > PHP讀取XML文件的方法實例總結【DOMDocument及simplexml方法】

PHP讀取XML文件的方法實例總結【DOMDocument及simplexml方法】

熱門標簽:廣州防封卡外呼系統多少錢一個月 廣東地市地圖標注 江西手機自動外呼防封系統是什么 長春人工外呼系統服務商 外呼系統撥打暫時無法接通 哪里辦理400電話 仁和怎么申請400開頭的電話 高德地圖標注家 怎么向銷售公司推銷外呼系統

本文實例講述了PHP讀取XML文件的方法。分享給大家供大家參考,具體如下:

使用DOMDocument對象讀取xml

創建一個DOMDocument對象

$doc = new DOMDocument();

載入xml文件

$doc->load("book.xml");

獲取標簽對象

$books = $doc->getElementsByTagName("book");

獲取標簽的子對象

$titles = $book->getElementsByTagName("title");

獲取標簽的值或屬性

$title = $titles->item(0)->nodeValue;

實例1,獲取圖書列表

book.xml

?xml version="1.0" encoding="UTF-8"?>
bookstore>
  book>
    title>PHP和MySQL開發/title>
    author>譚浩強/author>
  /book>
  book>
    titile>xml從入門到精通/titile>
    author>鄭智化/author>
  /book>
/bookstore>

load.php

?php
header("Content-type:text/html;charset=utf8");
$doc = new DOMDocument();  //創建DOMDocument對象
$doc->load("book.xml");  //打開book.xml
$books = $doc->getElementsByTagName("book"); //獲取book標簽對象
foreach ($books as $book){  //遍歷對象
  $titles = $book->getElementsByTagName("title");  //獲取book標簽下的title標簽
  $title = $titles->item(0)->nodeValue;  //獲取標簽的值
  $authors = $book->getElementsByTagName("author");//獲取book標簽下的author標簽
  $author = $authors->item(0)->nodeValue;  //獲取標簽的值
  $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();  //創建DOMDocument對象
$doc->load("config.xml");  //打開config.xml
$mysql = $doc->getElementsByTagName("mysql"); //獲取mysql標簽對象
$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);

PS:這里再為大家提供幾款關于xml操作的在線工具供大家參考使用:

在線XML/JSON互相轉換工具:
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

更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

希望本文所述對大家PHP程序設計有所幫助。

您可能感興趣的文章:
  • PHP中使用DOMDocument來處理HTML、XML文檔的示例
  • PHP創建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】
  • PHP基于DOMDocument解析和生成xml的方法分析
  • 如何解決php domdocument找不到的問題

標簽:湘西 濮陽 黔東 惠州 文山 海北 廈門 梅河口

巨人網絡通訊聲明:本文標題《PHP讀取XML文件的方法實例總結【DOMDocument及simplexml方法】》,本文關鍵詞  PHP,讀取,XML,文件,的,方法,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP讀取XML文件的方法實例總結【DOMDocument及simplexml方法】》相關的同類信息!
  • 本頁收集關于PHP讀取XML文件的方法實例總結【DOMDocument及simplexml方法】的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 西吉县| 古田县| 瓦房店市| 格尔木市| 上高县| 富蕴县| 呼伦贝尔市| 青海省| 曲阳县| 那坡县| 枞阳县| 托克逊县| 余江县| 普安县| 洪雅县| 惠水县| 铜陵市| 聂拉木县| 洞口县| 延川县| 葫芦岛市| 泉州市| 南靖县| 大埔县| 威海市| 玉树县| 赤城县| 韶关市| 高平市| 古浪县| 汪清县| 闽清县| 基隆市| 嘉荫县| 太和县| 长春市| 社旗县| 松桃| 高雄县| 静安区| 微博|