dskm94301
2011-05-16 17:53 阅读 34

帮助设置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>';
        }

        ?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doushoubu5360 doushoubu5360 2011-05-16 17:56
    <?php
        $xml = simplexml_load_file('http://events.stanford.edu/xml/mobile.xml');
    
        foreach($xml->Event as $event){
            echo '<h3>', $event->title, '</h3>';
        }
    
        ?>
    
    点赞 评论 复制链接分享
  • dongman5539 dongman5539 2011-05-16 18:17

    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.

    点赞 评论 复制链接分享

相关推荐