从xml读取php

时间:2015-03-08 08:54:32

标签: php xml

我有一个来自网址的数据点xml,最后有我的api密钥 所以它不是以.xml而是以数字结尾。

我上周已经尝试了解xml,但我感到困惑 当有多个id时,理想情况下我希望下面的xml在php中显示 当我用作包含时,我已经看过Simple XML了,但是我很容易感到困惑:/

我也尝试了这个,希望它可以做我想做的事,顺便说一句,我已经搜索过了 通过这样的许多小脚本

<?php
// The file test.xml contains an XML document with a root element
// and at least an element /[root]/title.

if (file_exists('metoforecast.xml')) {
    $xml = simplexml_load_file('metoforecast.xml');

    print_r($xml);
} else {
    exit('Failed to open metoforecast.xml.');
}
?>

我使用程序编辑抓取xml,用.xml文件名保存它 上传到我的网站,就像我说的,我感到困惑:(

我希望它看起来像这样

一些明亮或阳光明媚的咒语会在下午下雨。

今天: 一些明亮或阳光明媚的咒语,特别是在该地区的东部,但下午有更多的一般云和零星的雨。最高温度12C。

今晚: 任何下雨都会迅速清理干净的夜晚,在一些地方有一些明显的咒语和霜冻,尽管早晨云层会再次增加。最低温度0C。

星期一: 在干燥的开始之后,增厚的云将在稍后从西部带来一些零星的雨。然而,下午东部可能会出现一些更明亮的咒语。最高温度11C。

周二至周四的展望: 周二用一些阳光明媚的咒语擦干。周三早霜然后干燥而明亮,但到了晚上西边有雨。星期四下雨了。

英国对未来5天的预测 云和雨清除东南今天。明天潮湿多风。 今天: 最初在苏格兰和北爱尔兰充满阳光明媚的咒语和散乱的阵雨。在其他地方常常多云,威尔士和英格兰西北部的早期降雨在东南部缓慢蔓延并缓和。西北风力较大,天气较干燥,风力缓和。

今晚: 一个大部分干燥的夜晚,有漫长的清晰法术和微风,可以产生霜冻。在远西地区越来越多云,大雨和黎明时的强风。

星期一: 在东部开始明亮而寒冷。在北部和西北部转向潮湿多风,有严重大风的风险。凉风凛凛,南部有更多的雨水。

周二至周四的展望: 周二开始寒冷,然后大致细腻干燥。周三和周四的情况更加不稳定,有时会下雨,还有一些强风。

2015年3月12日(星期四)至2015年3月21日(星期六)的英国展望: 周四可能会有一些降雨会影响大部分地区,因为正面系统会向东移动穿越英国。在西部有时可能会下大雨,也可能有强风。雨可能在东部徘徊到星期五,但随着高压预计会发展,事情应该变得更加稳定。这在英国留下了很多干燥的天气,有一些明亮或阳光明媚的咒语,虽然可能有大量的云和奇怪的地方,大多是小雨或细雨。这种情况看起来会持续到下一周。虽然一些寒冷的夜晚仍有可能,但风一般应该很轻,温度接近一年中的平均温度。

2015年3月22日星期日至2015年4月5日星期日的英国展望: 我们很可能会在3月下旬到4月份逐渐减弱高压。这意味着北部地区可能会变得更加不稳定,有阵雨或更长时间的降雨。然而,在更南方,看起来条件设定为比平均值更干燥,春天的阳光更多。温度每天都有很大的变化,温暖的日子却是寒冷的夜晚。

于2015年3月8日星期日更新于:0252

<RegionalFcst xmlns="www.metoffice.gov.uk/xml/metoRegionalFcst" createdOn="2015-03-07T15:10:53" issuedAt="2015-03-07T16:00:00" regionId="ee">
<FcstPeriods>
<Period id="day1to2">
<Paragraph title="Headline:">Dry with increasing cloud. Mild although breezy.</Paragraph>
<Paragraph title="This Evening and Tonight:">
After a fine evening and clear start to the night cloud will thicken after midnight, though most parts will stay dry. Breezy winds will continue overnight. Minimum Temperature 5C.
</Paragraph>
<Paragraph title="Sunday:">
Generally cloudy with periods of rain and some brighter spells during the afternoon. Winds should ease and western parts may see a bright evening. Maximum Temperature 12C.
</Paragraph>
</Period>
<Period id="day3to5">
<Paragraph title="Outlook for Monday to Wednesday:">
Monday, a bright start but becoming windy with a spell of rain later. Tuesday should be fine with light winds. Wednesday starting fine but cloud thickening and wind strengthening later.
</Paragraph>
</Period>
<Period id="day6to15">
<Paragraph title="UK Outlook for Thursday 12 Mar 2015 to Saturday 21 Mar 2015:">
It is likely that some rain will affect most parts on Thursday as a frontal system tracks its way eastwards across the UK. The rain may be heavy at times in the west with strong winds also possible. The rain may linger in the east into Friday but things should then turn more settled as high pressure is expected to develop. This leaves a lot of dry weather over the UK with some bright or sunny spells though there may well be large amounts of cloud and the odd spot of mostly light rain or drizzle. This situation looks to persist into the following week. Winds should be generally light with temperatures close to average for the time of year, although some cold nights are still likely.
</Paragraph>
</Period>
<Period id="day16to30">
<Paragraph title="UK Outlook for Sunday 22 Mar 2015 to Sunday 5 Apr 2015:">
It is most probably that there will be a gradual weakening of the high pressure as we head through the latter part of March and into April. This means northern parts could well turn more unsettled, with showers or perhaps longer spells of rain. However, further south it looks like conditions are set to remain drier than average, with more spring sunshine. There is likely to be quite a large daily variation in temperatures, with warm days but chilly nights.
</Paragraph>
</Period>
</FcstPeriods>
</RegionalFcst>

你希望

巴希

1 个答案:

答案 0 :(得分:0)

您可以使用xpath方法从XML文件中获取所有Paragraph元素,然后简单地循环结果:

// Load XML. Suppress warning on namespace not being absolute
@$xml = simplexml_load_file('metoforecast.xml');

// Create prefix for namespace
$xml->registerXPathNamespace(
    "a", "www.metoffice.gov.uk/xml/metoRegionalFcst"
);

// Get all Paragraph items from the XML
$paragraphs = $xml->xpath('//a:Paragraph');

// Loop through Paragraph items, displaying title and forecast
while (list( , $paragraph) = each($paragraphs)) {
    // Begin paragraph
    echo '<p>', PHP_EOL;

    // Display title unless headline
    if ($paragraph['title'] != "Headline:")
        echo $paragraph['title'] , ' ';

    // Display forecast for the day
    echo trim($paragraph), PHP_EOL;

    // End paragraph
    echo '</p>', PHP_EOL, PHP_EOL;
}

使用上面提供的XML作为metoforecast.xml的内容运行上面的代码,您将获得以下输出(为了清晰起见,缩进):

<p>
    Dry with increasing cloud. Mild although breezy.
</p>

<p>
    This Evening and Tonight: After a fine evening and clear start to the night
    cloud will thicken after midnight, though most parts will stay dry. Breezy
    winds will continue overnight. Minimum Temperature 5C.
</p>

<p>
    Sunday: Generally cloudy with periods of rain and some brighter spells
    during the afternoon. Winds should ease and western parts may see a bright
    evening. Maximum Temperature 12C.
</p>

...