欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP讀取XML文件的方法實(shí)例總結(jié)【DOMDocument及simplexml方法】

 更新時(shí)間:2019年09月10日 09:55:03   作者:koastal  
這篇文章主要介紹了PHP讀取XML文件的方法,結(jié)合實(shí)例形式總結(jié)分析了php基于DOMDocument及simplexml方法針對(duì)xml文件的載入、讀取等相關(guān)操作技巧,需要的朋友可以參考下

本文實(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ì)有所幫助。

相關(guān)文章

最新評(píng)論