PHP读取XML文件的方法实例总结【DOMDocument及simplexml方法】
发布:smiling 来源: PHP粉丝网 添加日期:2021-12-16 11:15:09 浏览: 评论:0
本文实例讲述了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);
Tags: PHP读取XML DOMDocument
相关文章
- ·PHP如何读取xml文件?php读取xml文档例子(2014-08-20)
- ·php读取xml文件的三种实现方法(2014-08-20)
- ·php读取树型xml文件实现类(2014-08-20)
- ·php读取xml列表程序(2014-08-20)
- ·php读取xml文件(2014-08-21)
- ·如何在PHP中读取XML文件(2019-12-30)
- ·PHP 读取和编写 XML(2021-04-27)
- ·PHP中使用xmlreader读取xml数据示例(2021-05-05)
- ·PHP DOMDocument实现XML读写操作(2014-08-20)
- ·PHP中DOMDocument生成与解析XML格式数据(2014-08-20)
- ·php DOMDocument removeChild 删除节点函数(2014-08-20)
- ·PHP DOMDocument保存xml时中文出现乱码(2015-04-08)
- ·PHP创建XML的方法示例【基于DOMDocument类及SimpleXMLElement类】(2021-12-16)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)