php中simplexml_load_file函數(shù)用法實(shí)例
本文實(shí)例講述了php中simplexml_load_file函數(shù)用法。分享給大家供大家參考。具體用法分析如下:
在php中simplexml_load_file() 函數(shù)把 XML 文檔載入對(duì)象中之后我們就可以利用由此函數(shù)返回的對(duì)象進(jìn)行相關(guān)的操作了,下面我們看幾個(gè)測(cè)試實(shí)例.
例子,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.');
}
?>
運(yùn)行輸出結(jié)果如下:
object(SimpleXMLElement)#1 (4) {
["to"]=>
string(6) "George"
["from"]=>
string(4) "John"
["heading"]=>
string(8) "Reminder"
["body"]=>
string(25) "Don't forget the meeting!"
}
假如有一個(gè)“iciba.xml”文件,其內(nèi)容如下:
<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>氣球飄過(guò)天空。</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>日落西山時(shí),天空出現(xiàn)了一個(gè)移動(dòng)的發(fā)亮物體。</trans>
</sent>
<sent>
<orig>A bright rainbow arched above.</orig>
<trans>一彎明亮的彩虹懸掛在天空。</trans>
</sent>
</dict>
在PHP語(yǔ)言中我們可以用以下方法取得我們想要的值:
$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] => 氣球飄過(guò)天空。
)
[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] => 日落西山時(shí),天空出現(xiàn)了一個(gè)移動(dòng)的發(fā)亮物體。
)
[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.
翻譯:氣球飄過(guò)天空。
例句:A bolt of lightning lit up the sky.
翻譯:(一道)閃電照亮了天空。
例句:A bright moving object appeared in the sky at sunset.
翻譯:日落西山時(shí),天空出現(xiàn)了一個(gè)移動(dòng)的發(fā)亮物體。
例句:A bright rainbow arched above.
翻譯:一彎明亮的彩虹懸掛在天空。
例子,更深入的一個(gè)遍歷輸出生成表格,代碼如下:
$xml = simplexml_load_file('a.xml'); //載入xml文件 $lists和xml文件的根節(jié)點(diǎn)是一樣的
echo $xml->company."<br>";
echo $xml->town."<br>id:";
echo $xml->town['id']."<br>parent:";
echo $xml->town['parent']."<br>";
echo "<br>循環(huán)讀取:<br>";
foreach($xml->user as $users){ //有多個(gè)user,取得的是數(shù)組,循環(huán)輸出
echo "-------------------<br>";
echo "姓名:".$users->name."<br>";
echo "編號(hào):".$users->age."<br>";
echo "性別:".$users->age['sex']."<br>";
echo "序號(hào):".$users->height."<br>";
}
echo "<br>循環(huán)讀取:<br>";
foreach($xml->town as $towns){ //有多個(gè)user,取得的是數(shù)組,循環(huán)輸出
echo "-------------------<br>";
echo "id:".$towns['id']."<br>";
echo "歸屬:".$towns['parent']."<br>";
echo "地區(qū):".$towns."<br>";
}
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。
- PHP使用DOM和simplexml讀取xml文檔的方法示例
- php使用simplexml_load_file加載XML文件并顯示XML的方法
- php+xml編程之SimpleXML的應(yīng)用實(shí)例
- PHP中使用SimpleXML檢查XML文件結(jié)構(gòu)實(shí)例
- php的SimpleXML方法讀寫(xiě)XML接口文件實(shí)例解析
- php simplexmlElement操作xml的命名空間實(shí)現(xiàn)代碼
- PHP中simplexml_load_string函數(shù)使用說(shuō)明
- PHP XML操作的各種方法解析(比較詳細(xì))
- PHP中的生成XML文件的4種方法分享
- PHP基于SimpleXML生成和解析xml的方法示例
相關(guān)文章
取得單條網(wǎng)站評(píng)論以數(shù)組形式進(jìn)行輸出
這篇文章主要介紹了取得單條網(wǎng)站評(píng)論方法并以數(shù)組形式進(jìn)行輸出,需要的朋友可以參考下2014-07-07php urlencode()與urldecode()函數(shù)字符編碼原理詳解
中文字符編碼研究系列第五期,詳解 urlencode()與urldecode()函數(shù)字符編碼原理,兩個(gè)函數(shù)分別用于編碼 URL 字符串和解碼已編碼的 URL 字符串,實(shí)現(xiàn)對(duì)中文字符的編碼2011-12-12PHP 通過(guò)Socket收發(fā)十六進(jìn)制數(shù)據(jù)的實(shí)現(xiàn)代碼
以下是對(duì)PHP中通過(guò)Socket收發(fā)十六進(jìn)制數(shù)據(jù)的實(shí)現(xiàn)代碼進(jìn)行了分析介紹。需要的朋友可以過(guò)來(lái)參考下2013-08-08