本篇內(nèi)容介紹了“php中simplexml_load_string的使用方法”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到三門(mén)網(wǎng)站設(shè)計(jì)與三門(mén)網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋三門(mén)地區(qū)。先用一段代碼重現(xiàn)一下問(wèn)題
乍一看,結(jié)果很讓人費(fèi)解:
復(fù)制代碼 代碼如下:
<?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)解:
復(fù)制代碼 代碼如下:
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):
復(fù)制代碼 代碼如下:
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等方式獲取)
復(fù)制代碼 代碼如下:
<?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得到:
復(fù)制代碼 代碼如下:
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ǔ)言中可以用以下方法取得我們想要的值:
復(fù)制代碼 代碼如下:
<?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); //你好
?>
“php中simplexml_load_string的使用方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
網(wǎng)頁(yè)題目:php中simplexml_load_string的使用方法-創(chuàng)新互聯(lián)
文章出自:http://aaarwkj.com/article40/dppdho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、響應(yīng)式網(wǎng)站、企業(yè)網(wǎng)站制作、商城網(wǎng)站、自適應(yīng)網(wǎng)站、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容