没有从XML获取全部数据

时间:2019-06-19 12:35:55

标签: php html

我正在使用simplexml_load_file函数通过XML提取数据。我需要获取web_remarks数据。如果我使用file_get_contents,那么此数据将显示在inspect元素中。

urlpatterns = [
    # other patterns
    re_path(r'^account-confirm-email/(?P<key>[-:\w]+)/$', VerifyEmailView.as_view(),
            name='account_confirm_email'),
    re_path(r'^account-confirm-email/', VerifyEmailView.as_view(),
            name='account_email_verification_sent'),

]

我已经尝试了两种功能。

[Web_Remarks] => SimpleXMLElement Object


                   (
                    )

我只需要获取此web_remarks数据。谢谢

1 个答案:

答案 0 :(得分:0)

在PHP中处理XML响应的最简单方法是恕我直言,将其转换为数组。您可以使用simplexml_load_file($path)file_get_contents($path)simplexml_load_string($content)的组合。我更喜欢后者,因为它首先下载了内容,并且如果发生错误仍然允许我使用返回的内容。如果返回了有效的XML,一个简单的转换就是通过将其转换为JSON然后对其进行解码来略微绕行。之后,您可以使用一个简单的数组:

<?php
$file= 'http://xml.propspace.com/feed/xml.php?cl=1327&pid=8782&acc=8781';

$fileContent= file_get_contents($file);
try {
    $xml= simplexml_load_string($fileContent);
    $json = json_encode($xml);
    $array = json_decode($json,TRUE);

    print "<h2>Sample output</h3>".
      "Ad Type of Item 1 in 'Listing': " . $array["Listing"][0]["Ad_Type"]."<br />".
      "Title: <i>" . $array["Listing"][0]["Property_Title"]."</i><br />";


} catch (Exception $ex) {
    print "An exception occurred: " . $ex->getMessage();
}

这将为我的机器提供以下输出:

Sample output