在没有SimpleXML_load_file的PHP中获取XML

时间:2013-06-07 09:32:55

标签: php rss simplexml xml-namespaces

我正在尝试从RSS提要中获取信息,而且我已经使用PHP和Jquery设法达到了我想要的效果,但是我想知道它,现在我的应用程序开始使用.js,我的主持人说不能用cron开始。

如果我使用SimpleXML_load_file,它会带来一切,接受<item>....</item>和子<a10:updated>

中的属性

以下是原始Feed的一部分:

  

SimpleXMLElement对象   (       [@attributes] =&gt;排列           (               [版本] =&gt; 2.0           )

[channel] => SimpleXMLElement Object
    (
        [title] => Bills from current Parliamentary Session
        [link] => http://services.parliament.uk/bills
        [description] => SimpleXMLElement Object
            (
            )

        [item] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [guid] => http://services.parliament.uk/bills/2013-14/finance.html
                        [link] => http://services.parliament.uk/bills/2013-14/finance.html
                        [category] => Array
                            (
                                [0] => Commons
                                [1] => Government Bill
                            )

                        [title] => Finance
                        [description] => A Bill To grant certain duties, to alter other duties, and to amend the law relating to the National Debt and the Public Revenue, and to make further provision in connection with finance.
                    )

                [1] => SimpleXMLElement Object
                    (
                        [guid] => http://services.parliament.uk/bills/2013-14/humberbridge.html
                        [link] => http://services.parliament.uk/bills/2013-14/humberbridge.html
                        [category] => Array
                            (
                                [0] => Lords
                                [1] => Private Bill
                            )

                        [title] => Humber Bridge
                        [description] => A Bill to amend the constitution of the Humber Bridge Board and to confer new borrowing and other powers on it; to make new provision for the recovery of any deficit of the Board from local authorities in the area; to confer new powers for the setting and revision of tolls and to make other provision for and in connection with the operation of the bridge; and for connected purposes.
                    )

这是rss feed:http://services.parliament.uk/bills/AllBills.rss

还有另一种方法可以获得添加所有内容的原始RSS吗?

1 个答案:

答案 0 :(得分:2)

在SimpleXML对象上使用print_r()是不可靠的。它并不总是显示完整的结构。

您的<a10:updated>元素使用XML Namspaces,需要特殊处理。以下是如何访问它的示例:

$obj = simplexml_load_file(....);
foreach($obj->channel->item as $item)
{
    echo $item->children('http://www.w3.org/2005/Atom')->updated;
}

如您所见,您必须将a10命名空间的值传递给children()。命名空间定义可以在XML的顶部找到:

  

的xmlns:A10 = “http://www.w3.org/2005/Atom”