php Simplexml_Load_file解析xml详细实例
xml文档格式如下:
<?xml version="1.0" encoding="utf-8" <list> <company>武汉xxx公司</company> <user> <name>张三</name> <age sex="未知">a</age> <height>1</height> </user> <user> <name>李四</name> <age sex="女">b</age> <height>2</height> </user> <user> <name>王五</name> <age sex="男">c</age> <height>3</height> </user> <town parent="0" id="1">台北</town> <town parent="1" id="2">板桥</town> <town parent="0" id="3">桃园</town> </list>
Copy after login
php解析代码:
header("content-type:text/html; charset=utf-8"); //设置编码 $xml = simplexml_load_file('a.xml'); //载入xml文件 $lists和xml文件的根节点是一样的 echo $xml->company."<br>"; echo $xml->town."<br>id:"; echo $xml->town['id']."<br>parent:"; echo $xml->town['parent']."<br>"; echo "<br>循环读取:<br>"; foreach($xml->user as $users){ //有多个user,取得的是数组,循环输出 echo "-------------------<br>"; echo "姓名:".$users->name."<br>"; echo "编号:".$users->age."<br>"; echo "性别:".$users->age['sex']."<br>"; echo "序号:".$users->height."<br>"; } echo "<br>循环读取:<br>"; foreach($xml->town as $towns){ //有多个user,取得的是数组,循环输出 echo "-------------------<br>"; echo "id:".$towns['id']."<br>"; echo "归属:".$towns['parent']."<br>"; echo "地区:".$towns."<br>"; }
Copy after login
定义和用法:
simplexml_load_file() 函数把 xml 文档载入对象中,如果失败,则返回 false.
语法:simplexml_load_file(file,class,options,ns,is_prefix)
参数 描述
file 必需,规定要使用的 xml 文档.
class 可选,规定新对象的 class.
options 可选,规定附加的 libxml 参数.
ns 可选.
is_prefix 可选.
返回值:返回类 simplexmlelement 的一个对象,该对象的属性包含 xml 文档中的数据,如果失败,则返回 false.
本文地址:
转载随意,但请附上文章地址:-)
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
2 weeks ago
By DDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
CakePHP Tutorial
1377
52

