天天躁日日躁狠狠躁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使用實例分享,轉載需保留來源!

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

主站蜘蛛池模板: 2019香蕉在线观看直播视频 | 色午夜日本高清视频www | 一本道无码字幕在线看 | 亚洲国产韩国欧美在线不卡 | 99在线观看视频 | 伊人国产在线视频 | 亚洲伊人久久精品 | 国产99久久 | 久久精品热在线观看85 | 狠狠久久免费视频在线 | 国产人妻人伦精品熟女麻豆 | 国产乱辈通伦影片在线播放亚洲 | 久青草国产在视频在线观看 | 99久久蜜臀亚洲AV无码精品 | 久久这里只有精品视频9 | 久久亚洲精品中文字幕60分钟 | 91在线一区二区 | 亚洲乱码AV久久久久久久 | 女人久久WWW免费人成看片 | 99爱在线精品视频免费观看9 | 99国产精品久久久久久久日本竹 | 俄罗斯18xv在线观看 | 在线观看亚洲免费人成网址 | 翘臀后进美女白嫩屁股视频 | 国产精品人妻无码久久久蜜桃臀 | 色婷婷我要去我去也 | 强伦姧久久久久久久久久 | 年轻的老师5理伦片 | 国产91青青成人a在线 | 亚洲色图激情小说 | 男女作爱在线播放免费网页版观看 | 忘忧草直播 | 午夜福利免费院 | 两个人的视频hd全免费 | 精品久久伊人 | 伊人久久精品99热超碰 | 97无码欧美熟妇人妻蜜 | 久久精品一区二区三区资源网 | 亚洲精品午夜aaa级久久久久 | 欧美三级在线完整版免费 | 精油按摩日本 |