如何从PHP中的@xml属性检索数据 [英] How to retrieve data from @ xml attribute in PHP

查看:25
本文介绍了如何从PHP中的@xml属性检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我在 PHP 的东西中被这个 xml 卡住了.考虑到我一生中头 3 个小时一起学习 XML,我已经走得很远了.

Ok so I am stuck with this xml in PHP stuff. I have gotten pretty far considering its my first 3 hours into XML all together ever in my entire life.

我无法从名称中包含 @ 的 XML 事物中提取数据.见下文(显然我不会发布整个 XML 内容,但你可以看到我是如何在下面看到的.

I am having trouble pulling data from a XML thing that has @ in the name. See below (obviously im not going to post the whole XML thing but u can see how i got there below that.

SimpleXMLElement Object
(
[@attributes] => Array
    (
        [date] => 2010-09
        [reserved] => 6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
    )

)

我是如何到达那里的:

echo $this->General_functions->naked($xml->property[0]->availability->month[0]);

General_functions->naked 只是一个快速的函数来包装和打印给定的属性.

General_functions->naked is just a fast function to wrap and print_r around the given attribute.

我的问题是,我如何获取@attributes 中的值,因为无论我尝试什么,我都无法弄清楚.我在网上搜索了 45 分钟,但没有真正的答案.

My question is, HOW do i get the values inside @attributes cause no matter what i try i cant figure it out. Ive searched the web for a good 45 mins with no real answer.

提前致谢.大卫

推荐答案

您需要使用 attributes() 方法来获取作为另一个类的结果.因此,例如,要获取 date 属性:

You need to use the attributes() method to get the results as another class. So, for example, to get the date attribute:

$myElement->attributes()->date

另请注意,它不是字符串,而是 SimpleXML 属性.如果你想得到它的实际值,你需要明确地将它转换为 string :

Also note that it's not a string, it's a SimpleXML attribute. If you want to get its actual value, you need to cast it to string explicitly:

(string)$myElement->attributes()->date

这篇关于如何从PHP中的@xml属性检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆