Ryan
Ryan

Reputation: 6227

Help setting up PHP SimpleXML

I'm having trouble getting PHP's SimpleXML to work with our XML feed. I'm just calling the title attribute for simplification. When I run any of this code it only exports empty h3 tags. Any help is greatly appreciated.

I've tried this:

        <?php
        $xml = simplexml_load_file('http://events.stanford.edu/xml/mobile.xml');

        foreach($xml as $event){
            echo '<h3>', $event['title'], '</h3>';
        }

        ?>

...and this:

        <?php
        $xml = simplexml_load_file('http://events.stanford.edu/xml/mobile.xml');

        foreach($xml->Event as $event){
            echo '<h3>', $event['title'], '</h3>';
        }

        ?>

...and this:

        <?php
        $xml = simplexml_load_file('http://events.stanford.edu/xml/mobile.xml');

        foreach($xml as $node){
            echo '<h3>', $node['title'], '</h3>';
        }

        ?>

Upvotes: 0

Views: 152

Answers (2)

fbstj
fbstj

Reputation: 1714

You are using the object $event as an array, which does not work, either do as the other answers say and reference it as an object ($event->title) or convert it to an array (cast? ((array)$event)['title']. I'd suggest the first.

I sense that you're used to javascript objects which can be indexed as hash tables, whereas in PHP arrays are completely different to objects.

Upvotes: 0

Danzan
Danzan

Reputation: 968

<?php
    $xml = simplexml_load_file('http://events.stanford.edu/xml/mobile.xml');

    foreach($xml->Event as $event){
        echo '<h3>', $event->title, '</h3>';
    }

    ?>

Upvotes: 2

Related Questions