php中simplexml_load_string使用實(shí)例分享
先用一段代碼重現(xiàn)一下問(wèn)題
乍一看,結(jié)果很讓人費(fèi)解:
<?php
$string = <<<EOF
<data>
<foo><bar>hello</bar></foo>
<foo><bar>world</bar></foo>
</data>
EOF;
$data = simplexml_load_string($string);
print_r($data);
print_r($data->foo);
?>
乍一看,結(jié)果很讓人費(fèi)解:
SimpleXMLElement Object
(
[foo] => Array
(
[0] => SimpleXMLElement Object
(
[bar] => hello
)
[1] => SimpleXMLElement Object
(
[bar] => world
)
)
)
SimpleXMLElement Object
(
[bar] => hello
)
明明print_r顯示foo是一個(gè)有兩個(gè)bar元素的數(shù)組,但是最后卻僅僅顯示了一個(gè)bar元素!
原因其實(shí)很簡(jiǎn)單,在如上所示simplexml_load_string的結(jié)果里,foo并不是數(shù)組,而是一個(gè)迭代對(duì)象!
可以這樣確認(rèn):
foreach ($data->foo as $v) print_r($v);
foreach ($data->children() as $v) print_r($v);
看來(lái),print_r或者var_dump之類的表象并不完全可信,自己多留心吧。
假如我們獲取的XML數(shù)據(jù)如下:(可以使用curl、fsockopen等方式獲?。?/P>
<?xml version="1.0" encoding="UTF-8"?>
<dict num="219" id="219" name="219">
<key>你好</key>
<pos></pos>
<acceptation>Array;Array;Array;</acceptation>
<sent>
<orig>Haven't seen you for a long time. How are you?</orig>
<trans>多日不見(jiàn)了,你好嗎?</trans>
</sent>
<sent>
<orig>Hello! How are you?</orig>
<trans>嘿,你好?</trans>
</sent>
<sent>
<orig>Hello, Brooks!How are you?</orig>
<trans>喂,布魯克斯!你好嗎?</trans>
</sent>
<sent>
<orig>Hi, Barbara, how are you?</orig>
<trans>嘿,芭芭拉,你好嗎?</trans>
</sent>
<sent>
<orig>How are you? -Quite well, thank you.</orig>
<trans>你好嗎?-很好,謝謝你。</trans>
</sent>
</dict>
經(jīng)過(guò)simplexml_load_string得到:
SimpleXMLElement Object
(
[@attributes] => Array
(
[num] => 219
[id] => 219
[name] => 219
)
[key] => 你好
[pos] => SimpleXMLElement Object
(
)
[acceptation] => Array;Array;Array;
[sent] => Array
(
[0] => SimpleXMLElement Object
(
[orig] => Haven't seen you for a long time. How are you?
[trans] => 多日不見(jiàn)了,你好嗎?
)
[1] => SimpleXMLElement Object
(
[orig] => Hello! How are you?
[trans] => 嘿,你好?
)
[2] => SimpleXMLElement Object
(
[orig] => Hello, Brooks!How are you?
[trans] => 喂,布魯克斯!你好嗎?
)
[3] => SimpleXMLElement Object
(
[orig] => Hi, Barbara, how are you?
[trans] => 嘿,芭芭拉,你好嗎?
)
[4] => SimpleXMLElement Object
(
[orig] => How are you? -Quite well, thank you.
[trans] => 你好嗎?-很好,謝謝你。
)
)
)
我們?cè)赑HP語(yǔ)言中可以用以下方法取得我們想要的值:
<?php
$data = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<dict num="219" id="219" name="219">
<key>你好</key>
<pos></pos>
<acceptation>Array;Array;Array;</acceptation>
<sent>
<orig>Haven't seen you for a long time. How are you?</orig>
<trans>多日不見(jiàn)了,你好嗎?</trans>
</sent>
<sent>
<orig>Hello! How are you?</orig>
<trans>嘿,你好?</trans>
</sent>
<sent>
<orig>Hello, Brooks!How are you?</orig>
<trans>喂,布魯克斯!你好嗎?</trans>
</sent>
<sent>
<orig>Hi, Barbara, how are you?</orig>
<trans>嘿,芭芭拉,你好嗎?</trans>
</sent>
<sent>
<orig>How are you? -Quite well, thank you.</orig>
<trans>你好嗎?-很好,謝謝你。</trans>
</sent>
</dict>
XML;
$xmldata = simplexml_load_string($data);
header("Content-Type: text/html; charset=UTF-8");
print_r($xmldata);
echo "<br />".trim($xmldata->sent[0]->orig); //Haven't seen you for a long time. How are you?
echo "<br />".trim($xmldata->key); //你好
?>
相關(guān)文章
PHP 中 DOMDocument保存xml時(shí)中文出現(xiàn)亂碼問(wèn)題的解決方案
這篇文章主要介紹了PHP 中 DOMDocument保存xml時(shí)中文出現(xiàn)亂碼問(wèn)題的解決方案,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09四種php中webservice實(shí)現(xiàn)的簡(jiǎn)單架構(gòu)方法及實(shí)例
這篇文章主要介紹了四種php中webservice實(shí)現(xiàn)的簡(jiǎn)單架構(gòu)方法及實(shí)例,需要的朋友可以參考下2015-02-02php數(shù)組轉(zhuǎn)換js數(shù)組操作及json_encode的用法詳解
php數(shù)組轉(zhuǎn)換js數(shù)組操作及json_encode的用法。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10yii2.0數(shù)據(jù)庫(kù)遷移教程【多個(gè)數(shù)據(jù)庫(kù)同時(shí)同步數(shù)據(jù)】
這篇文章主要介紹了yii2.0數(shù)據(jù)庫(kù)遷移的方法,可實(shí)現(xiàn)多個(gè)數(shù)據(jù)庫(kù)同時(shí)同步數(shù)據(jù)的功能,較為詳細(xì)的分析了Yii2針對(duì)遷移的創(chuàng)建、提交、重做及自定義遷移的相關(guān)概念與使用方法,需要的朋友可以參考下2016-10-10php查看請(qǐng)求頭信息獲取遠(yuǎn)程圖片大小的方法分享
php通過(guò)fsockopen方法獲取頭信息,如果請(qǐng)求的是圖片,這里的Content-Length就表示圖片的大小2013-12-12服務(wù)器上配置PHP運(yùn)行環(huán)境教程
這篇文章主要介紹了如何在服務(wù)器上配置PHP運(yùn)行環(huán)境的方法及相關(guān)配置,十分全面,這里推薦給大家。2015-02-02