欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php中simplexml_load_string使用实例分享

程序员文章站 2023-11-23 18:11:04
先用一段代码重现一下问题 乍一看,结果很让人费解: 复制代码 代码如下:...

先用一段代码重现一下问题

乍一看,结果很让人费解:

复制代码 代码如下:

<?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, *s!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, *s!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, *s!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); //你好
?>