天天躁日日躁狠狠躁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等方式獲取)

復制代碼 代碼如下:
<?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使用實例分享,轉載需保留來源!

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

主站蜘蛛池模板: 国产精品久久毛片A片软件爽爽 | 99热只有这里有精品 | 国内精品久久久久影院男同志 | 再深点灬舒服灬太大了在线视频 | 在线天天看片视频免费观看 | 99视频偷窥在线精品国自产拍 | 日本十八禁无遮拦啪啪漫画 | 日本高清加勒比 | 在公交车上被JB草坏了被轮J了 | 国产野外无码理论片在线观看 | 精品国产麻豆免费人成网站 | 国产福利一区二区精品 | 日操夜操天天操 | 色老99九久精品偷偷鲁 | 妇少水多18P蜜泬17P亚洲乱 | 国内卡一卡二卡三免费网站 | 91嫩草视频在线观看 | 日韩精品一卡二卡三卡四卡2021 | 成人免费视频在线看 | 女人高潮时一吸一夹 | 国产99小视频 | 亚洲高清国产拍精品动图 | 男人的天堂色 | 草莓视频在线观看完整高清免费 | 偷柏自拍亚洲综合在线 | 亚洲 欧美 日本 国产 高清 | 亚洲免费每日在线观看 | 国产午夜伦鲁鲁 | 久久亚洲高清观看 | 国产一区日韩二区欧美三区 | 中文字幕精品AV内射夜夜夜 | 99免费精品| 99久久婷婷国产麻豆精品电影 | 动漫女主被扒开双腿羞辱 | 色欲AV亚洲永久无码精品 | 超嫩校花被灌醉在线观看 | 欧美一级情欲片在线 | 日本女人下面毛茸茸 | 亚洲精品久久国产高清 | 轻轻挺进女教师的身体 | 日本粉嫩学生毛绒绒 |