本篇内容介绍了“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中simplexml_load_string的使用方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
网页题目:php中simplexml_load_string的使用方法-创新互联
文章出自:https://www.cdcxhl.com/article40/dppdho.html
成都网站建设公司_创新互联,为您提供标签优化、响应式网站、企业网站制作、商城网站、自适应网站、网站内链
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联