xpath php attributes not working?

Posted by Jared on Stack Overflow See other posts from Stack Overflow or by Jared
Published on 2012-09-04T21:16:46Z Indexed on 2012/09/04 21:38 UTC
Read the original article Hit count: 143

Filed under:
|
|

Getting this error Call to a member function attributes() on a non-object

I have found multiple answers to this on SO, but none of them seem to solve my problem?

Here is the XML:

<Routes>
    <Route type="source" name="incoming">
    </Route>
<Routes>

Here is the PHP:

$doc = new SimpleXMLElement('routingConfig.xml', null, true);
class traverseXML {

    function getData() {
        global $doc;
        $routeCount = count($doc -> xpath("Route")); //this value returns correctly
        $routeArr = array();
        for ($i = 1; $i <= $routeCount; $i++) {

            $name = $doc -> Route[$i] -> attributes() -> name;

            array_push($routeArr, $name);

        }
        return $routeArr;

    }

    }
    $traverseXML = new traverseXML;
    var_dump($traverseXML -> getData());

I understand what the error means, but how is it a non-object? How do I return the name attribute of Routes/Route[1] ?

© Stack Overflow or respective owner

Related posts about php

Related posts about xpath