“尝试获取非对象的属性” SimpleXML 和 PHP 错误

发布于 2024-08-25 00:46:39 字数 1974 浏览 1 评论 0原文

我使用带有 SimpleXML 的 PHP 脚本来解析 XML 提要。我无法控制 XML 的内容。

   try { $xml = @new SimpleXMLElement($fetchResult); } catch (Exception $e) { errorHandle($e->getMessage());}

    $userNick = $xml->View->ScrollView->VBoxView->View->MatrixView->VBoxView[0]->HBoxView->TextView->SetFontStyle->b;
    foreach ($xml->View->ScrollView->VBoxView->View->MatrixView->VBoxView[0]->VBoxView as $pathToSubTree){       
        foreach ($pathToSubTree->MatrixView->View->VBoxView->VBoxView->HBoxView[0]->VBoxView->MatrixView->VBoxView as $canopy){
                //Do some stuff now that we've found the canopy of the tree
        }

        $canopy = $pathToSubTree->MatrixView->View->VBoxView->VBoxView->HBoxView[0]->VBoxView->MatrixView->VBoxView;
        if(is_null($canopy)){
                //Do some stuff stuff is the canopy was not traceable   
        }
    }

    $pathToSubTree = $xml->View->ScrollView->VBoxView->View->MatrixView->VBoxView[0]->VBoxView;
    if(is_null($pathToSubTree)){
         //Do some stuff stuff is the subTree path was not traceable    
    }

    unset($xml);

我收到很多两个错误,我确信它们与相同的原因有关:

PHP Notice:  Trying to get property of non-object in myScript.php on line 45
PHP Warning:  Invalid argument supplied for foreach() in myScript.php on line 45
PHP Notice:  Trying to get property of non-object in myScript.php on line 76

第 45 行是(从上面):

foreach ($pathToSubTree->MatrixView->View->VBoxView->VBoxView->HBoxView[0]->VBoxView->MatrixView->VBoxView as $canopy){

第 76 行是(从上面):

$canopy = $pathToSubTree->MatrixView->View->VBoxView->VBoxView->HBoxView[0]->VBoxView->MatrixView->VBoxView;

我很确定这个错误是由其中之一引起的我的路径中的数组不是特定 XML 的数组,但有时它可以是数组。

处理这些问题的正确方法是什么?

I'm using a PHP script with SimpleXML to parse an XML feed. I have no control over the content of the XML.

   try { $xml = @new SimpleXMLElement($fetchResult); } catch (Exception $e) { errorHandle($e->getMessage());}

    $userNick = $xml->View->ScrollView->VBoxView->View->MatrixView->VBoxView[0]->HBoxView->TextView->SetFontStyle->b;
    foreach ($xml->View->ScrollView->VBoxView->View->MatrixView->VBoxView[0]->VBoxView as $pathToSubTree){       
        foreach ($pathToSubTree->MatrixView->View->VBoxView->VBoxView->HBoxView[0]->VBoxView->MatrixView->VBoxView as $canopy){
                //Do some stuff now that we've found the canopy of the tree
        }

        $canopy = $pathToSubTree->MatrixView->View->VBoxView->VBoxView->HBoxView[0]->VBoxView->MatrixView->VBoxView;
        if(is_null($canopy)){
                //Do some stuff stuff is the canopy was not traceable   
        }
    }

    $pathToSubTree = $xml->View->ScrollView->VBoxView->View->MatrixView->VBoxView[0]->VBoxView;
    if(is_null($pathToSubTree)){
         //Do some stuff stuff is the subTree path was not traceable    
    }

    unset($xml);

I'm getting lots of two errors, which I'm sure are related to the same cause:

PHP Notice:  Trying to get property of non-object in myScript.php on line 45
PHP Warning:  Invalid argument supplied for foreach() in myScript.php on line 45
PHP Notice:  Trying to get property of non-object in myScript.php on line 76

Line 45 is (from above):

foreach ($pathToSubTree->MatrixView->View->VBoxView->VBoxView->HBoxView[0]->VBoxView->MatrixView->VBoxView as $canopy){

Line 76 is (from above):

$canopy = $pathToSubTree->MatrixView->View->VBoxView->VBoxView->HBoxView[0]->VBoxView->MatrixView->VBoxView;

I'm pretty sure this error is caused by one of the arrays in my path not being an array for the particular XML, but some times it CAN be an array.

What's the correct way to deal with these?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆之渊 2024-09-01 00:46:39

for 标签可以出现多次或单独出现:

is_array($doc->node) ? $doc->node[0] : $doc->node

或者它可能更容易使用:

$node->xpath('MatrixView/View/XBoxView/VBoxView/HBoxView[1]/VBoxView/MatrixView/VBoxView)

[1] 是第一个匹配的元素

for tags can appear multiple times or singularly:

is_array($doc->node) ? $doc->node[0] : $doc->node

or it might be easier to use:

$node->xpath('MatrixView/View/XBoxView/VBoxView/HBoxView[1]/VBoxView/MatrixView/VBoxView)

the [1] is the first element matched

落在眉间の轻吻 2024-09-01 00:46:39

以下是对这些错误消息的解释:

PHP Warning:  Invalid argument supplied for foreach() in myScript.php on line 45

这个很简单。您传递了一些无法迭代到 foreach 的内容,例如 foreach (false as $x)
在您的情况下,您的疯狂长系列 $foo->bar->baz 可能会返回 null,因为该元素不存在。

PHP Notice:  Trying to get property of non-object in myScript.php on line 45

“尝试获取属性”你肯定知道对象的属性“bar”写成->bar,“非对象”指的是-><之前的变量/代码>。这意味着 $xml->View->ScrollView->... 中的某处有一个不存在的元素,并且 SimpleXML 返回 null。因此,下一个 -> 会触发该错误。

简而言之,您的元素“路径”是错误的。

Here's an explanation of those error messages:

PHP Warning:  Invalid argument supplied for foreach() in myScript.php on line 45

This one is easy. You've passed something that couldn't be iterated over to foreach, such as foreach (false as $x).
In your case, your crazy-long series of $foo->bar->baz probably returns null because the element doesn't exist.

PHP Notice:  Trying to get property of non-object in myScript.php on line 45

"Trying to get property" you surely know that an object's property "bar" is written ->bar, "of non-object" refers to the variable right before the ->. It means that somewhere in your $xml->View->ScrollView->... there's an element that doesn't exists and SimpleXML returns null. In consequence the next -> triggers that error.

In short, your "path" to the element is wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文