色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

php中simplexml_load_string使用實例分享

先用一段代碼重現一下問題

乍一看,結果很讓人費解:

復制代碼 代碼如下:
<?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);
?>

乍一看,結果很讓人費解:

復制代碼 代碼如下:
SimpleXMLElement Object
(
[foo] => Array
(
[0] => SimpleXMLElement Object
(
[bar] => hello
)
[1] => SimpleXMLElement Object
(
[bar] => world
)
)
)
SimpleXMLElement Object
(
[bar] => hello
)

明明print_r顯示foo是一個有兩個bar元素的數組,但是最后卻僅僅顯示了一個bar元素!
原因其實很簡單,在如上所示simplexml_load_string的結果里,foo并不是數組,而是一個迭代對象!
可以這樣確認:

復制代碼 代碼如下:
foreach ($data->foo as $v) print_r($v);
foreach ($data->children() as $v) print_r($v);

看來,print_r或者var_dump之類的表象并不完全可信,自己多留心吧。

假如我們獲取的XML數據如下:(可以使用curl、fsockopen等方式獲取)

復制代碼 代碼如下:
<?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>多日不見了,你好嗎?</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>

經過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] => 多日不見了,你好嗎?
                )

            [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] => 你好嗎?-很好,謝謝你。
                )

        )

)

我們在php語言中可以用以下方法取得我們想要的值:

復制代碼 代碼如下:
<?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>多日不見了,你好嗎?</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技術php中simplexml_load_string使用實例分享,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 涟水县| 黔南| 双鸭山市| 谷城县| 金华市| 绿春县| 平定县| 东阳市| 库尔勒市| 东安县| 郑州市| 平山县| 广西| 陵川县| 杨浦区| 东明县| 新闻| 伊通| 丰台区| 泽库县| 琼海市| 易门县| 上饶县| 永济市| 丹棱县| 东至县| 黄龙县| 金门县| 永登县| 尤溪县| 观塘区| 双辽市| 思茅市| 吉木乃县| 铜山县| 惠安县| 稷山县| 抚远县| 修水县| 廊坊市| 类乌齐县|