简体   繁体   English

帮助设置PHP SimpleXML

[英]Help setting up PHP SimpleXML

I'm having trouble getting PHP's SimpleXML to work with our XML feed . 我无法让PHP的SimpleXML与我们的XML feed一起使用 I'm just calling the title attribute for simplification. 我只是为了简化而调用title属性。 When I run any of this code it only exports empty h3 tags. 当我运行任何此代码时,它只导出空的h3标记。 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>';
        }

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

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

    ?>

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. 您正在使用对象$event作为数组,这不起作用,或者像其他答案所说的那样做并将其作为对象引用( $event->title )或将其转换为数组(cast? ((array)$event)['title'] 。我建议第一个。

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. 我觉得你已经习惯了可以被索引为哈希表的javascript对象,而在PHP中,数组与对象完全不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM