【问题标题】:Checking if XML Attributes exist in loop检查循环中是否存在 XML 属性
【发布时间】:2017-09-25 16:14:26
【问题描述】:

在遍历下面的 xml 对象时,我需要从每个属性中获取数据,如果属性不存在,则继续。我不确定如何检查@attributes 是否存在。目前,如果它没有我的循环死亡。

我试过了:

  if(isset($v[0]->user->attributes())){
        ....
  but that gives me a php error:

Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

所以我不确定如何检查它是否存在,如果存在我可以通过循环运行。如果不行,我可以继续……

 [server] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [id] => 17980
                    )

                [user] => Array
                    (
                        [0] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [id] => 1075159
                                        [default-loc] => 50000
                                        [status] => 1
                                        [license] => high,standard
                                        [password] => aebecf880f2060c31e44f820785e0aa63ea4cc44
                                        [primary] => 50000
                                        [username] => 17980_50000
                                    )

                                [description] => Customer X
              )
         )
    )
     [1] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [id] => 14642
                    )

                [user] => Array
                      (
                        [0] => SimpleXMLElement Object
                            (
                              [description] => Customer Y
                            )
    )
     [2] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [id] => 24151
                    )

                [user] => Array
                    (
                        [0] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [id] => 1075159
                                        [default-loc] => 50000
                                        [status] => 1
                                        [license] => high,low
                                        [password] => aebecf880f2060c31e44f820785e0aa63ea4cc44
                                        [primary] => 51412
                                        [username] => 17980_51412
                                    )

                                [description] => Customer Z
              )
         )
    )
)

【问题讨论】:

  • 与您的问题无关,但您的密码似乎使用 SHA1 进行了哈希处理。请注意,这对于现代安全目的不够

标签: php xml


【解决方案1】:

我不确定这对您有多大帮助,但在尝试遍历 XML 时。我发现最好使用递归而不是循环。您的问题没有显示您使用的是递归函数还是循环,但您确实说“当循环...”

如果您以递归方式解决此问题,则只要检测到您位于类型属性的叶节点处,您就可以简单地停止并执行所需的操作。

这应该消除您在简单地“循环”通过 XML 时可能遇到的任何潜在的空指针异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 2023-04-03
    • 2018-09-27
    • 2011-11-03
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多