tags:

views:

841

answers:

2

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?

+1  A: 

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

So it would be equivalent to do:$node->xpath('MatrixView[1]/View[1]/XBoxView[1]/VBoxView[1]/HBoxView[1]/VBoxView[1]/MatrixView[1]/VBoxView[1]) ?
SooDesuNe
yep, and if those other tags can only appear singularly, you prolly wouldn't need [1] on those.
+1  A: 

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.

Josh Davis