2016-08-20 104 views
0

我正在使用Node.js框架在AWS上开发。我从这个XML抓住天气数据:解析节点js的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://www.nws.noaa.gov/forecasts/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">2016-08-20T18:44:02Z</creation-date> 
    </product> 
    <source> 
     <more-information>http://www.nws.noaa.gov/forecasts/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.02"/> 
    </location> 
    <location> 
     <location-key>point2</location-key> 
     <point latitude="39.70" longitude="-104.80"/> 
    </location> 
    <location> 
     <location-key>point3</location-key> 
     <point latitude="47.60" longitude="-122.30"/> 
    </location> 
    <moreWeatherInformation applicable-location="point1">http://forecast.weather.gov/MapClick.php?textField1=38.99&amp;textField2=-77.02</moreWeatherInformation> 
    <moreWeatherInformation applicable-location="point2">http://forecast.weather.gov/MapClick.php?textField1=39.70&amp;textField2=-104.80</moreWeatherInformation> 
    <moreWeatherInformation applicable-location="point3">http://forecast.weather.gov/MapClick.php?textField1=47.60&amp;textField2=-122.30</moreWeatherInformation> 
    <time-layout time-coordinate="local" summarization="none"> 
     <layout-key>k-p24h-n1-1</layout-key> 
     <start-valid-time>2016-08-20T08:00:00-04:00</start-valid-time> 
     <end-valid-time>2016-08-20T20:00:00-04:00</end-valid-time> 
    </time-layout> 
    <time-layout time-coordinate="local" summarization="none"> 
     <layout-key>k-p24h-n1-2</layout-key> 
     <start-valid-time>2016-08-20T20:00:00-04:00</start-valid-time> 
     <end-valid-time>2016-08-21T09:00:00-04:00</end-valid-time> 
    </time-layout> 
    <time-layout time-coordinate="local" summarization="none"> 
     <layout-key>k-p24h-n1-3</layout-key> 
     <start-valid-time>2016-08-20T08:00:00-06:00</start-valid-time> 
     <end-valid-time>2016-08-20T20:00:00-06:00</end-valid-time> 
    </time-layout> 
    <time-layout time-coordinate="local" summarization="none"> 
     <layout-key>k-p24h-n1-4</layout-key> 
     <start-valid-time>2016-08-20T20:00:00-06:00</start-valid-time> 
     <end-valid-time>2016-08-21T09:00:00-06:00</end-valid-time> 
    </time-layout> 
    <time-layout time-coordinate="local" summarization="none"> 
     <layout-key>k-p24h-n1-5</layout-key> 
     <start-valid-time>2016-08-20T08:00:00-07:00</start-valid-time> 
     <end-valid-time>2016-08-20T20:00:00-07:00</end-valid-time> 
    </time-layout> 
    <time-layout time-coordinate="local" summarization="none"> 
     <layout-key>k-p24h-n1-6</layout-key> 
     <start-valid-time>2016-08-20T20:00:00-07:00</start-valid-time> 
     <end-valid-time>2016-08-21T09:00:00-07:00</end-valid-time> 
    </time-layout> 
    <parameters applicable-location="point1"> 
     <temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n1-1"> 
     <name>Daily Maximum Temperature</name> 
     <value>90</value> 
     </temperature> 
     <temperature type="minimum" units="Fahrenheit" time-layout="k-p24h-n1-2"> 
     <name>Daily Minimum Temperature</name> 
     <value>73</value> 
     </temperature> 
    </parameters> 
    <parameters applicable-location="point2"> 
     <temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n1-3"> 
     <name>Daily Maximum Temperature</name> 
     <value>76</value> 
     </temperature> 
     <temperature type="minimum" units="Fahrenheit" time-layout="k-p24h-n1-4"> 
     <name>Daily Minimum Temperature</name> 
     <value>53</value> 
     </temperature> 
    </parameters> 
    <parameters applicable-location="point3"> 
     <temperature type="maximum" units="Fahrenheit" time-layout="k-p24h-n1-5"> 
     <name>Daily Maximum Temperature</name> 
     <value>90</value> 
     </temperature> 
     <temperature type="minimum" units="Fahrenheit" time-layout="k-p24h-n1-6"> 
     <name>Daily Minimum Temperature</name> 
     <value>61</value> 
     </temperature> 
    </parameters> 
    </data> 
</dwml> 

我有这个在我的文件的顶部:

/** module used for parsing XML easily. https://www.npmjs.com/package/xml2js*/ 
var parseString = require('xml2js').parseString; 

那么我的主要调用此函数:

function getNoaa(){ 
    var baseURL = "http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php?listLatLon="; 

    // Indianapolis 
    var originLat = 39.7683800; 
    var originLng = -86.1580400; 

    // Chicago 
    var destLat = 41.881832; 
    var destLng = -87.623177; 

    baseURL = baseURL.concat(originLat); 
    baseURL = baseURL.concat(","); 
    baseURL = baseURL.concat(originLng); 
    baseURL = baseURL.concat(","); 

    baseURL = baseURL.concat(destLat); 
    baseURL = baseURL.concat(","); 
    baseURL = baseURL.concat(destLng); 

    // TIME 
    baseURL = baseURL.concat("&product=time-series&begin=2016-08-19T00:00:00&end=2016-08-20T00:00:00"); 

    // CHARACTERISTICS REQUESTED 
    // http://graphical.weather.gov/xml/docs/elementInputNames.php 
    baseURL = baseURL.concat("&Unit=e&maxt=maxt&mint=mint&temp=temp&appt=appt&rh=rh"); 

    console.log(baseURL); 

    request(baseURL, function (error, response, body) 
    { 
     if (!error && response.statusCode == 200) 
     { 
      //console.log(body); 

      // parse the XML 
      parseString(body, function (err, result) { 
       //console.dir(result); 

       console.log(result["dwml"]["data"]["location"][1]["point"].latitude); 
      }); 
     } 
    }) 
} 

的一部分,多数民众赞成失败是这条线:

console.log(result["dwml"]["data"]["location"][1]["point"].latitude); 

这是错误代码我得到:

2016-08-20T18:37:49.654Z 312eb295-6705-11e6-9e3c-ff815be91875 TypeError: Cannot read property '1' of undefined 
    at /var/task/index.js:109:51 
    at Parser.<anonymous> (/var/task/node_modules/xml2js/lib/xml2js.js:489:18) 
    at emitOne (events.js:77:13) 
    at Parser.emit (events.js:169:7) 
    at Object.onclosetag (/var/task/node_modules/xml2js/lib/xml2js.js:447:26) 
    at emit (/var/task/node_modules/sax/lib/sax.js:640:35) 
    at emitNode (/var/task/node_modules/sax/lib/sax.js:645:5) 
    at closeTag (/var/task/node_modules/sax/lib/sax.js:905:7) 
    at Object.write (/var/task/node_modules/sax/lib/sax.js:1449:13) 
    at Parser.exports.Parser.Parser.parseString (/var/task/node_modules/xml2js/lib/xml2js.js:508:31) 

我基本上想知道我在做什么毛病得到data父节点内部的第二(或第一为此事)location节点。我希望能够获得latitudelongitude元素值。

回答

1

来自xml2js的解析数据与xml不是1:1,因为在xml中允许多次使用相同的名称(键)。

正确console.log第二位点是:

console.log(result["dwml"]["data"][0]["location"][1]["point"][0]["$"].latitude); 

你也可以跳过括号键:

console.log(result.dwml.data[0].location[1].point[0].$.latitude); 

希望这有助于。

+0

我不知道是否可以跳过括号,因为“位置”是一个受保护的词。但我会在一秒之内全部尝试,谢谢! – booky99

+0

位置是一个受保护的词,而不是受保护的属性@ booky99 – baao