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

php中simplexml_load_string使用實(shí)例分享

先用一段代碼重現(xià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í)很簡單,在如上所示simplexml_load_string的結(jié)果里,foo并不是數(shù)組,而是一個(gè)迭代對象!
可以這樣確認(rèn):

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

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

假如我們獲取的XML數(shù)據(jù)如下:(可以使用curl、fsockopen等方式獲?。?/P>

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

            [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語言中可以用以下方法取得我們想要的值:

復(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>多日不見了,你好嗎?</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技術(shù)php中simplexml_load_string使用實(shí)例分享,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 亚洲无线观看国产 | 久久黄色免费网站 | 99久久综合 | 成人欧美一区二区三区白人 | 免费看国产曰批40分钟 | 99热久久视频只有精品6国产 | 香蕉99久久久久成人麻豆 | 性生生活大片又黄又 | 亚洲AV午夜福利精品香蕉麻豆 | 国产激情视频在线观看 | 欧美丝袜女同 | 免费观看亚洲视频 | 最近更新2019中文字幕免费 | 日本久久频这里精品99 | 久久sese | 亚洲精品高清在线 | WWW夜片内射视频在观看视频 | 精品免费在线视频 | 日韩欧美成人免费中文字幕 | 精品国产精品人妻久久无码五月天 | 日本十八禁无遮无挡漫画 | 野花日本大全免费高清完整版 | 国产精彩视频在线 | 三叶草成人 | 亚洲精品天堂自在久久77 | 免费中文字幕视频 | 免费精品国产人妻国语麻豆 | 性色香蕉AV久久久天天网 | 久久大胆视频 | 国产精品久久毛片A片软件爽爽 | 亚洲精品福利一区二区在线观看 | 8x8x我要打机飞在线观看 | 日本久久中文字幕精品 | 久久久久久91香蕉国产 | 色偷拍自怕亚洲在线 | 果冻传媒2021在线观看 | 丹麦1o一19sex性hdhd | 日本午夜精品一区二区三区电影 | 久久无码av三级 | 美女张开腿露尿口给男人亲 | 国产精品无码亚洲网 |