Reading php from an xml


Reading php from an xml

我有一个来自URL的数据点XML,它的末尾有我的API密钥所以它不是以.xml结尾,而是以数字结尾。

上周我一直在尝试理解xml,但我感到困惑当有多个ID时,理想情况下我希望下面的XML以PHP显示当用作包含时,我看过简单的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。

星期一:在干燥的开始之后,变厚的云层稍后会从西方带来一些斑驳的降雨。然而,下午东部可能会有一些更明亮的咒语。最高温度11°C。

周二至周四展望:周二晒干,天气晴朗。周三的早霜,然后干燥明亮,但傍晚西部有雨。周四一些地方还会下雨。

英国未来5天的预测今天东南方向云雨晴朗。明天潮湿多风。今天:起初,苏格兰和北爱尔兰阳光明媚,阵雨分散。其他地方经常多云,威尔士和英格兰西北部的早雨缓慢向东南蔓延并减弱。天气从西北方向开始,天气更加干燥,风力减弱。

今晚:一个大部分干燥的夜晚,漫长的晴朗咒语和微风使霜冻发展。远西地区多云量越来越大,大雨和黎明时分风力增强。

星期一:明亮但寒冷,从东方开始。变得潮湿和多风,北部和西北部有大风的风险。微风拂面,南方多

雨。

周二至周四展望:周二开始寒冷,然后基本上是晴朗和干燥的。周三和周四变得更加不安,有时会下雨,风力会增强。

2015年3月

12日(星期四)至2015年3月21日(星期六)的英国展望:周四可能会有一些降雨影响大部分地区,因为锋面系统向东穿过英国。西部有时可能会下大雨,也可能有强风。降雨可能会在东部持续到周五,但随着预计高压的发展,事情应该会变得更加稳定。这给英国留下了很多干燥的天气,有一些明亮或阳光明媚的天气,尽管很可能有大量的云层和大部分小雨或毛毛雨的奇怪地点。这种情况似乎会持续到下周。风通常应该很轻,温度接近一年中的平均水平,尽管仍然可能有一些寒冷的夜晚。

2015年

3月22日星期日至2015年4月5日星期日的英国展望:最有可能的是,随着我们进入3月下旬和4月,高压将逐渐减弱。这意味着北部地区很可能会变得更加不稳定,有阵雨或更长的降雨时间。然而,再往南看,情况似乎仍将比平均水平更干燥,春季阳光更多。每天的温度变化可能很大,白天温暖,夜晚寒冷。

更新于: 0252 于 周日 08 Mar 2015

<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>

希望你的

巴希

您可以使用 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>
...