帮助设置 PHP SimpleXML
我无法让 PHP 的 SimpleXML 与我们的 XML feed 配合使用。为了简化起见,我只是调用 title 属性。当我运行任何此代码时,它仅导出空的 h3 标签。非常感谢任何帮助。
我已经尝试过这个:
<?php
$xml = simplexml_load_file('http://events.stanford.edu/xml/mobile.xml');
foreach($xml as $event){
echo '<h3>', $event['title'], '</h3>';
}
?>
...还有这个:
<?php
$xml = simplexml_load_file('http://events.stanford.edu/xml/mobile.xml');
foreach($xml->Event as $event){
echo '<h3>', $event['title'], '</h3>';
}
?>
...还有这个:
<?php
$xml = simplexml_load_file('http://events.stanford.edu/xml/mobile.xml');
foreach($xml as $node){
echo '<h3>', $node['title'], '</h3>';
}
?>
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>';
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用对象
$event
作为数组,这不起作用,要么按照其他答案所说的那样并将其引用为对象($event->title
)或将其转换为数组(强制转换?((array)$event)['title']
。我建议第一个。我感觉您已经习惯了 javascript 对象,这些对象可以是索引为哈希表,而在 PHP 中数组与对象完全不同。
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.