博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php simplexml_load_file 详解,php中simplexml_load_file函数用法实例讲解
阅读量:6903 次
发布时间:2019-06-27

本文共 3443 字,大约阅读时间需要 11 分钟。

php中simplexml_load_file函数用法实例讲解

发布于 2015-02-07 06:53:40 | 136 次阅读 | 评论: 0 | 来源: 网友投递

PHP开源脚本语言PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适用于Web开发领域。PHP的文件后缀名为php。

本文为大家讲解的是php中simplexml_load_file函数用法实例讲解,感兴趣的同学参考下。

在php中simplexml_load_file() 函数把 XML 文档载入对象中之后我们就可以利用由此函数返回的对象进行相关的操作了,下面我们看几个测试实例.

例子,XML文件代码如下:

George

John

Reminder

PHP 代码如下:

if (file_exists('test.xml'))

{

$xml = simplexml_load_file('test.xml');

var_dump($xml);

}

else

{

exit('Error.');

}

?>

运行输出结果如下:

object(SimpleXMLElement)#1 (4) {

["to"]=>

string(6) "George"

["from"]=>

string(4) "John"

["heading"]=>

string(8) "Reminder"

["body"]=>

string(25) "Don't forget the meeting!"

}

假如有一个“iciba.xml”文件,其内容如下:

天空

Array;Array;

The church tower stood against the sky like a finger pointing towards heaven.

教堂的尖塔在天空的映衬下宛如指向天空的手指。

A balloon floated across the sky.

气球飘过天空。

A bolt of lightning lit up the sky.

(一道)闪电照亮了天空。

A bright moving object appeared in the sky at sunset.

日落西山时,天空出现了一个移动的发亮物体。

A bright rainbow arched above.

一弯明亮的彩虹悬挂在天空。

在PHP语言中我们可以用以下方法取得我们想要的值:

$xmldata = simplexml_load_file("iciba.xml");

header("Content-Type: text/html; charset=UTF-8");

print_r($xmldata); //第一部分

$listcount = count($xmldata->sent);

for($i=0;$i

$dictlist = $xmldata->sent[$i];

echo "

例句:".$dictlist->orig;

echo "

翻译:".$dictlist->trans;

}

?>

“第一部分”将输出:

SimpleXMLElement Object

(

[@attributes] => Array

(

[num] => 219

[id] => 219

[name] => 219

)

[key] => 天空

[pos] => SimpleXMLElement Object

(

)

[acceptation] => Array;Array;

[sent] => Array

(

[0] => SimpleXMLElement Object

(

[orig] => The church tower stood against the sky like a finger pointing towards heaven.

[trans] => 教堂的尖塔在天空的映衬下宛如指向天空的手指。

)

[1] => SimpleXMLElement Object

(

[orig] => A balloon floated across the sky.

[trans] => 气球飘过天空。

)

[2] => SimpleXMLElement Object

(

[orig] => A bolt of lightning lit up the sky.

[trans] => (一道)闪电照亮了天空。

)

[3] => SimpleXMLElement Object

(

[orig] => A bright moving object appeared in the sky at sunset.

[trans] => 日落西山时,天空出现了一个移动的发亮物体。

)

[4] => SimpleXMLElement Object

(

[orig] => A bright rainbow arched above.

[trans] => 一弯明亮的彩虹悬挂在天空。

)

)

)

“第二部分”将输出:

例句:The church tower stood against the sky like a finger pointing towards heaven.

翻译:教堂的尖塔在天空的映衬下宛如指向天空的手指。

例句:A balloon floated across the sky.

翻译:气球飘过天空。

例句:A bolt of lightning lit up the sky.

翻译:(一道)闪电照亮了天空。

例句:A bright moving object appeared in the sky at sunset.

翻译:日落西山时,天空出现了一个移动的发亮物体。

例句:A bright rainbow arched above.

翻译:一弯明亮的彩虹悬挂在天空。

例子,更深入的一个遍历输出生成表格,代码如下:

eader("content-type:text/html; charset=utf-8"); //设置编码

$xml = simplexml_load_file('a.xml'); //载入xml文件 $lists和xml文件的根节点是一样的

echo $xml->company."

";

echo $xml->town."

id:";

echo $xml->town['id']."

parent:";

echo $xml->town['parent']."

";

echo "

循环读取:
";

foreach($xml->user as $users){ //有多个user,取得的是数组,循环输出

echo "-------------------

";

echo "姓名:".$users->name."

";

echo "编号:".$users->age."

";

echo "性别:".$users->age['sex']."

";

echo "序号:".$users->height."

";

}

echo "

循环读取:
";

foreach($xml->town as $towns){ //有多个user,取得的是数组,循环输出

echo "-------------------

";

echo "id:".$towns['id']."

";

echo "归属:".$towns['parent']."

";

echo "地区:".$towns."

";

}

相关阅读:

php中simplexml_load_file函数用法实例讲解

php中opendir函数用法实例讲解

php中chdir()函数用法示例讲解

php中fgetcsv()函数用法实例

php内嵌函数用法实例讲解

PHP中curl_setopt函数用法实例分析

php中get_defined_constants函数用法实例分析

PHP中list()函数用法实例简析

PHP中spl_autoload_register()函数用法实例详解

php中HTTP_REFERER函数用法详解及实例

PHP中soap的用法实例

php的dl函数用法实例

转载地址:http://rtodl.baihongyu.com/

你可能感兴趣的文章
去中心化
查看>>
win10安装MarkdownPad 2报错This view has crashed的处理及md简单语法
查看>>
Unity3D - 设计模式 - 工厂模式
查看>>
第二十六课:jQuery对事件对象的修复
查看>>
Leetcode题目:Swap Nodes in Pairs
查看>>
Windows聚焦转为图片
查看>>
POJ NOI0101-09 字符菱形
查看>>
jQuery--停止动画和判断是否处于动画状态stop()
查看>>
iPhone 物理尺寸与分辨率
查看>>
win10系统下cmd输入一下安装的软件命令提示拒绝访问解决办法
查看>>
OpenJudge/Poj 1004 Financial Management
查看>>
面向服务(接口)开发过程中常用的实体类数据复制解决方案
查看>>
Scala 学习笔记之函数(3)
查看>>
Fiddler (二) Script 用法
查看>>
[转载] 信息系统项目管理师考试习题——整体管理
查看>>
LC 425 word squares
查看>>
Web框架之Django_08 重要组件(form组件、cookie和session组件)
查看>>
HDU 6103
查看>>
zookeeper 实现分布式锁
查看>>
sql server 数据修改不了的设计
查看>>