0

我刚开始研究 NDFD REST 服务以获取天气数据。简而言之,我不知道如何将特定参数链接到返回 XML 中的时间布局元素。

示例调用:http ://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php?lat=38.99&lon=-77.01&product=time-series&temp=temp&maxt=maxt&mint=mint

我正在使用此调用查找最高温度(maxt)、最低温度(mint)和 3 小时温度(temp)。

我得到的 XML 是(为了相关性而剪掉的):

<?xml version="1.0"?>
<dwml version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd">
  <head>
    <product srsName="WGS 1984" concise-name="time-series" operational-mode="official">
      <title>NOAA's National Weather Service Forecast Data</title>
      <field>meteorological</field>
      <category>forecast</category>
      <creation-date refresh-frequency="PT1H">2012-12-26T20:03:47Z</creation-date>
    </product>
    <source>
      <more-information>http://graphical.weather.gov/xml/</more-information>
      <production-center>Meteorological Development Laboratory<sub-center>Product Generation Branch</sub-center></production-center>
      <disclaimer>http://www.nws.noaa.gov/disclaimer.html</disclaimer>
      <credit>http://www.weather.gov/</credit>
      <credit-logo>http://www.weather.gov/images/xml_logo.gif</credit-logo>
      <feedback>http://www.weather.gov/feedback.php</feedback>
    </source>
  </head>
  <data>
    <location>
      <location-key>point1</location-key>
      <point latitude="38.99" longitude="-77.01"/>
    </location>
    <moreWeatherInformation applicable-location="point1">http://forecast.weather.gov/MapClick.php?textField1=38.99&amp;textField2=-77.01</moreWeatherInformation>
    <time-layout time-coordinate="local" summarization="none">
      <layout-key>k-p24h-n7-1</layout-key>
      <start-valid-time>2012-12-26T07:00:00-05:00</start-valid-time>
      <end-valid-time>2012-12-26T19:00:00-05:00</end-valid-time>
    </time-layout>
    <time-layout time-coordinate="local" summarization="none">
      <layout-key>k-p24h-n6-2</layout-key>
      <start-valid-time>2012-12-26T19:00:00-05:00</start-valid-time>
      <end-valid-time>2012-12-27T08:00:00-05:00</end-valid-time>
    </time-layout>
    <time-layout time-coordinate="local" summarization="none">
      <layout-key>k-p3h-n34-3</layout-key>
      <start-valid-time>2012-12-26T16:00:00-05:00</start-valid-time>
      <start-valid-time>2012-12-26T19:00:00-05:00</start-valid-time>
      <start-valid-time>2012-12-26T22:00:00-05:00</start-valid-time>
      <start-valid-time>2012-12-27T01:00:00-05:00</start-valid-time>
    </time-layout>
    <parameters applicable-location="point1">
      <temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n7-1">
        <name>Daily Maximum Temperature</name>
        <value>34</value>
      </temperature>
      <temperature type="minimum" units="Fahrenheit" time-layout="k-p24h-n6-2">
        <name>Daily Minimum Temperature</name>
        <value>34</value>
      </temperature>
      <temperature type="hourly" units="Fahrenheit" time-layout="k-p3h-n34-3">
        <name>Temperature</name>
        <value>33</value>
        <value>34</value>
        <value>34</value>
        <value>34</value>
      </temperature>
    </parameters>
  </data>
</dwml>

现在,我想将time-layout元素和元素的子元素链接parameters到 maxt、mint 和 temp,以便我可以相应地填充我的模型对象。

有没有人做过类似的事情?

现在,我认为我可以发出 53 个服务调用(每个参数一个,如 maxt、mint、temp 等),但对于多个纬度/经度对(我有很多),这不会很好地扩展。

我将不胜感激任何建议。

4

3 回答 3

1

每个元素都包含一个 layout-key 元素,它提供了用于表示该时间布局的名称。元素的每个子元素都有一个时间布局属性,该属性的值告诉您它与哪个时间布局对齐。

http://www.nws.noaa.gov/mdl/XML/Design/MDL_XML_Design.pdf_orig中的第 5.2 节值得一读。这是一个示例名称:“k-p3h-n28-1”和 5.2 告诉我们“k”代表“key”,“p3h”代表 3 小时的时间段(至少最初是),n28 表示有 28 个values 提供,最后 -1 是这样,这将是一个唯一标签。通常您不想解析这些标签,只需使用 time-layout 元素中包含的实际时间列表,因为间隔可能会随着您的变化而变化通过数据移动。

相同的方案被用于将位置与数据相关联。这些元素有一个 location-key 子元素,它定义了一个像“point1”这样的标签,然后元素有一个适用的位置属性来将它们与它们的位置联系起来。

于 2014-04-08T14:14:28.817 回答
0

我认为有两种方法可以解决这个问题:1)具有适合 NDFD 数据结果的自定义类的反序列化器。

您可以在https://msdn.microsoft.com/en-us/library/x6c1kb0s%28v=vs.110%29.aspx尝试 XML 架构定义工具 (Xsd.exe)

2)使用LINQ解析XML文档如(c#)

System.Xml.Linq.XDocument xmlDoc = System.Xml.Linq.XDocument.Parse(xmlData);

var recordTime = from timevalue in xmlDoc.Descendants("time-layout").Elements("start-valid-time")
                         where timevalue.Parent.Element("layout-key").Value.Contains("k-p3h")
                         select (DateTime)timevalue;

var maxTemperature = from tempvalue in xmlDoc.Descendants("temperature").Elements("value")
                             where tempvalue.Parent.Attribute("type").Value == "maximum"
                          select (double)tempvalue;

var minTemperature = from tempvalue in xmlDoc.Descendants("temperature").Elements("value")
                             where tempvalue.Parent.Attribute("type").Value == "minimum"
                          select (double)tempvalue;

var temperature = from tempvalue in xmlDoc.Descendants("temperature").Elements("value")
                       where tempvalue.Parent.Attribute("type").Value == "hourly"
                       select (double)tempvalue;

var humidity = from humvalue in xmlDoc.Descendants("humidity").Elements("value")
                       where humvalue.Parent.Attribute("type").Value == "relative"
                       select (double)humvalue;
于 2015-03-27T13:51:29.693 回答
0

规范在这里:http ://www.nws.noaa.gov/mdl/XML/Design/MDL_XML_Design.pdf您可能已经看到了。一般来说,事情应该排队。例如,在您的代码段中,您有 4 个 start-valid-time 元素,它们应该对应于 4 个温度。我注意到,特别是当它接近时间间隔更改时,数据可能会在不同时间更新,并且某些元素可能不一致或具有值 xsi:nil="true"。

于 2013-02-25T17:33:47.043 回答