天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

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等方式獲?。?/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>多日不見了,你好嗎?</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使用實例分享,轉載需保留來源!

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

主站蜘蛛池模板: 亚洲欧美日韩高清专区 | 99精彩视频在线观看 | 久久99视热频国只有精品 | 大胆国模一区二区三区伊人 | 艳鉧动漫片1~6全集在线 | 亚洲AV国产国产久青草 | 日本A级作爱片金瓶双艳 | 小柔的性放荡羞辱日记 | 久久激情网 | 果冻传媒在线观看进入窗口 | 亚洲免费片 | 亚洲日本香蕉视频观看视频 | 姐姐不~不可以动漫在线观看 | 亚洲AV无码A片在线观看蜜桃 | 国产人成无码视频在线观看 | 色 花 堂 永久 网站 | 最新无码国产在线视频 | 青青久久精品 | 就操成人网 | 日韩视频在线观看 | yellow2019在线观看视频 | 伊人久久大香线蕉综合网站 | 亚洲国产成人久久一区www妖精 | 日产2021免费一二三四区在线 | 99精品国产福利在线观看 | 伊人久久大香线蕉综合高清 | 桃花色影院 | 口内射精颜射极品合集 | 久久国语露脸精品国产 | 麻豆国产原创中文AV网站 | 武侠古典久久亚洲精品 | ppypp午夜限制不卡影院私人 | 芳草地在线观看免费视频 | 久久久免费观看 | 中文字幕在线观看网站 | 亚洲视频在线观看不卡 | 亚洲综合AV色婷婷五月蜜臀 | 999久久久无码国产精蜜柚 | 在线a亚洲视频 | 亚洲AV无码A片在线观看蜜桃 | 一区二区三区内射美女毛片 |