如何从该 xml 对象中提取值?
object(SimpleXMLElement)#1 (3) {
["@attributes"]=> array(1) {
["responsecode"]=> string(3) "200"
}
["nextpage"]=> object(SimpleXMLElement)#2 (0) {
}
["resultset_web"]=> object(SimpleXMLElement)#3 (2) {
["@attributes"]=> array(4) {
["count"]=> string(2) "10"
["start"]=> string(1) "0"
["totalhits"]=> string(8) "22497060"
["deephits"]=> string(8) "23000000"
}
["result"]=> array(10) {
[0]=> object(SimpleXMLElement)#4 (7) {
["abstract"]=> string(110) "MSN's all-in-one Internet portal, the home of Hotmail, MSN Messenger, MSNBC News, Encarta, and Slate Magazine."
["clickurl"]=> string(360) "http://teqpad.com/www/msn.com"
["date"]=> string(10) "2011/01/14"
["dispurl"]=> object(SimpleXMLElement)#14 (0) {
}
["size"]=> string(5) "82136"
["title"]=> string(3) "MSN"
["url"]=> string(19) "http://www.msn.com/"
}
我想使用 php 从上面的 xml 中提取 title
和 abstract
的值。
object(SimpleXMLElement)#1 (3) {
["@attributes"]=> array(1) {
["responsecode"]=> string(3) "200"
}
["nextpage"]=> object(SimpleXMLElement)#2 (0) {
}
["resultset_web"]=> object(SimpleXMLElement)#3 (2) {
["@attributes"]=> array(4) {
["count"]=> string(2) "10"
["start"]=> string(1) "0"
["totalhits"]=> string(8) "22497060"
["deephits"]=> string(8) "23000000"
}
["result"]=> array(10) {
[0]=> object(SimpleXMLElement)#4 (7) {
["abstract"]=> string(110) "MSN's all-in-one Internet portal, the home of Hotmail, MSN Messenger, MSNBC News, Encarta, and Slate Magazine."
["clickurl"]=> string(360) "http://teqpad.com/www/msn.com"
["date"]=> string(10) "2011/01/14"
["dispurl"]=> object(SimpleXMLElement)#14 (0) {
}
["size"]=> string(5) "82136"
["title"]=> string(3) "MSN"
["url"]=> string(19) "http://www.msn.com/"
}
I want to extract value of title
and abstract
from above xml using php.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您已经使用 SimpleXML 解析了它,您真正想要做的是遍历对象并找到
title
和abstract
的值。如果您的对象是
$xml
,则$xml->resultset_web->result[0]->abstract
包含abstract
而$xml->resultset_web->result[0]->title
包含一个值的title
。对于所有的值,You have already parsed it with SimpleXML, what you really want to do is traverse through your object and find the values of
title
andabstract
.If your object is
$xml
,$xml->resultset_web->result[0]->abstract
containsabstract
while$xml->resultset_web->result[0]->title
containstitle
for one value. For all the values,据此判断,我可以看到您大约有 10 个标题可供提取:
这样做的方法是:
I can see that you have about 10 titles to extract by judging from this:
The way of doing this would be:
这看起来像是保存 SimpleXML 对象的 var 的 var_dump。
假设您有这样的东西:
那么您应该能够访问这样的项目:
That looks like a var_dump of a var that holds a SimpleXML object.
So let's say that you have something like this:
then you should be able to access the items like this:
如果你想获取路径为resultset_web的所有节点->结果->标题:
If you want to get all nodes with path resultset_web -> result -> title:
未经测试,但应该可以工作:
正如所说,只需阅读 php simplexml 文档即可。请注意,并非每种情况都这么简单,如果您有多个结果,则必须使用 Children() 方法迭代子级,您无法像我上面那样直接访问它们。
Untested, but should work :
As said, just read up on the php simplexml documentation. And note that not each case will be this simple, if you have multiple results you will have to iterate through the children with the children() method, you can't access them directly as I did above.