展会信息港展会大全

php中simplexml_load_file函数使用
来源:互联网   发布日期:2016-03-01 14:27:38   浏览:2066次  

导读:例子 XML 文件: 代码如下 ?xml version=1.0 encoding=ISO-8859-1? note toGeorge/to fromJohn/from headingReminder/heading bodyDon't forget the meeting!/body /note PHP 代码: ?php if (file_exists('test.xml')) { $xml = simplexml_load_file('test....

例子

XML 文件:

代码如下

<?xmlversion="1.0"encoding="ISO-8859-1"?>

<note>

<to>George</to>

<from>John</from>

<heading>Reminder</heading>

<body>Don'tforget the meeting!</body>

</note>

PHP代码:

<?php

if(file_exists('test.xml'))

{

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

var_dump($xml);

}

else

{

exit('Error.');

}

?>

输出:

object(SimpleXMLElement)#1(4)

{

["to"]=> string(4) "George"

["from"]=> string(4)"John"

["heading"]=> string(8) "Reminder"

["body"]=> string(29)"Don't forget the meeting!"

}

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

代码如下

<?xmlversion="1.0" encoding="UTF-8"?>

<dict num="219" id="219"name="219">

<key>天空</key>

<pos></pos>

<acceptation>Array;Array;</acceptation>

<sent>

<orig>Thechurch tower stood against the sky like a finger pointing towardsheaven.</orig>

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

</sent>

<sent>

<orig>Aballoon floated across thesky.</orig>

<trans>气球飘过天空。</trans>

</sent>

<sent>

<orig>Abolt of lightning lit up thesky.</orig>

<trans>(一道)闪电照亮了天空。</trans>

</sent>

<sent>

<orig>Abright moving object appeared in the sky atsunset.</orig>

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

</sent>

<sent>

<orig>Abright rainbow archedabove.</orig>

<trans>一弯明亮的彩虹悬挂在天空。</trans>

</sent>

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

<?php

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

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

print_r($xmldata); //第一部分www.111cn.net

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

for($i=0;$i<$listcount;$i++){//第二部分

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

echo "<br/>例句:".$dictlist->orig;

echo "<br/>翻译:".$dictlist->trans;

}

?>“第一部分”将输出:

SimpleXMLElementObject

(

[@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 skylike 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 atsunset.

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

)

[4] => SimpleXMLElement Object

(

[orig] => A bright rainbow arched above.

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

)

)

)“第二部分”将输出:

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

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

例句: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."<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>";

}

赞助本站

人工智能实验室

相关热词: 开发 编程 android

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港