php中simplexml_load_file函数用法实例
本文实例讲述了php中simplexml_load_file函数用法。分享给大家供大家参考。具体用法分析如下:
在php中simplexml_load_file() 函数把 xml 文档载入对象中之后我们就可以利用由此函数返回的对象进行相关的操作了,下面我们看几个测试实例.
例子,xml文件代码如下:
<note>
<to>george</to>
<from>john</from>
<heading>reminder</heading>
<body>don't forget the meeting!</body>
</note>
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”文件,其内容如下:
<dict num="219" id="219" name="219">
<key>天空</key>
<pos></pos>
<acceptation>array;array;</acceptation>
<sent>
<orig>the church tower stood against the sky like a finger pointing towards heaven.</orig>
<trans>教堂的尖塔在天空的映衬下宛如指向天空的手指。</trans>
</sent>
<sent>
<orig>a balloon floated across the sky.</orig>
<trans>气球飘过天空。</trans>
</sent>
<sent>
<orig>a bolt of lightning lit up the sky.</orig>
<trans>(一道)闪电照亮了天空。</trans>
</sent>
<sent>
<orig>a bright moving object appeared in the sky at sunset.</orig>
<trans>日落西山时,天空出现了一个移动的发亮物体。</trans>
</sent>
<sent>
<orig>a bright rainbow arched above.</orig>
<trans>一弯明亮的彩虹悬挂在天空。</trans>
</sent>
</dict>
在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<$listcount;$i++){ //第二部分
$dictlist = $xmldata->sent[$i];
echo "<br />例句:".$dictlist->orig;
echo "<br />翻译:".$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.
翻译:一弯明亮的彩虹悬挂在天空。
例子,更深入的一个遍历输出生成表格,代码如下:
$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>";
}
希望本文所述对大家的php程序设计有所帮助。
上一篇: php一维二维数组键排序方法实例总结