php中simplexml_load_file函數用法執行個體,simplexml_PHP教程

來源:互聯網
上載者:User

php中simplexml_load_file函數用法執行個體,simplexml


本文執行個體講述了php中simplexml_load_file函數用法。分享給大家供大家參考。具體用法分析如下:

在php中simplexml_load_file() 函數把 XML 文檔載入對象中之後我們就可以利用由此函數返回的對象進行相關的操作了,下面我們看幾個測試執行個體.

例子,XML檔案代碼如下:
複製代碼 代碼如下:<?xml version="1.0" encoding="ISO-8859-1"?>

George
John
Reminder
Don't forget the meeting!

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(6) "George"
["from"]=>
string(4) "John"
["heading"]=>
string(8) "Reminder"
["body"]=>
string(25) "Don't forget the meeting!"
}

假如有一個“iciba.xml”檔案,其內容如下:
複製代碼 代碼如下:<?xml version="1.0" encoding="UTF-8"?>

天空

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語言中我們可以用以下方法取得我們想要的值:
複製代碼 代碼如下:<?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 "
例句:".$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程式設計有所協助。

http://www.bkjia.com/PHPjc/910605.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/910605.htmlTechArticlephp中simplexml_load_file函數用法執行個體,simplexml 本文執行個體講述了php中simplexml_load_file函數用法。分享給大家供大家參考。具體用法分析如下: 在...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.