simplexml获取没有类型转换的节点值 [英] simplexml get node value without typecasting

查看:199
本文介绍了simplexml获取没有类型转换的节点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从 simplexml 对象获取节点值而不转换它?

Is there any way to get a node value from a simplexml object without casting it?

$amount = (int)$item->amount;

这在我看来不是很漂亮,我在寻找一个更干净的方式,但没有找到任何东西到目前为止!

This is not very beautiful in my opinion, I'm searching for a cleaner way but did not find anything so far!

//wouldn't this be nice?
$amount = $item->amount->getValue();

提前感谢。

推荐答案

获取一个节点的值,而不必进行类型转换?当然! :3

Getting the value of a node without having to typecast it? Sure thing! :3

class SimplerXMLElement extends SimpleXMLElement
{
    public function getValue()
    {
        return (string) $this;
    }
}

现在你只需要使用 simplexml_load_string()的第二个参数来获得更简单的XML元素!

Now you just have to use simplexml_load_string()'s second parameter to get even simpler XML elements!

是节点。如果你需要使用节点的值作为字符串,整数和什么不是,它将涉及typecasting在一个点或另一个,无论你是否亲自。

More seriously though, a node is a node. If you need to use the value of a node as a string, integer and what not, it will involve typecasting at one point or another, whether you do it personally or not.

这篇关于simplexml获取没有类型转换的节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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