0
<?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">2013-10-29T16:59:47Z</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="39.95" longitude="-75.17"/>
    </location>
    <moreWeatherInformation applicable-location="point1">http://forecast.weather.gov/MapClick.php?textField1=39.95&amp;textField2=-75.17</moreWeatherInformation>
    <time-layout time-coordinate="local" summarization="none">
      <layout-key>k-p24h-n1-1</layout-key>
      <start-valid-time>2013-10-30T08:00:00-04:00</start-valid-time>
      <end-valid-time>2013-10-30T20:00:00-04:00</end-valid-time>
    </time-layout>
    <time-layout time-coordinate="local" summarization="none">
      <layout-key>k-p24h-n2-2</layout-key>
      <start-valid-time>2013-10-29T20:00:00-04:00</start-valid-time>
      <end-valid-time>2013-10-30T09:00:00-04:00</end-valid-time>
      <start-valid-time>2013-10-30T20:00:00-04:00</start-valid-time>
      <end-valid-time>2013-10-31T09:00:00-04:00</end-valid-time>
    </time-layout>
    <time-layout time-coordinate="local" summarization="none"> 
      <layout-key>k-p3h-n9-3</layout-key>
      <start-valid-time>2013-10-30T02:00:00-04:00</start-valid-time>
      <start-valid-time>2013-10-30T05:00:00-04:00</start-valid-time>
      <start-valid-time>2013-10-30T08:00:00-04:00</start-valid-time>
      <start-valid-time>2013-10-30T11:00:00-04:00</start-valid-time>
      <start-valid-time>2013-10-30T14:00:00-04:00</start-valid-time>
      <start-valid-time>2013-10-30T17:00:00-04:00</start-valid-time>
      <start-valid-time>2013-10-30T20:00:00-04:00</start-valid-time>
      <start-valid-time>2013-10-30T23:00:00-04:00</start-valid-time>
      <start-valid-time>2013-10-31T02:00:00-04:00</start-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>64</value>
      </temperature>
      <temperature type="minimum" units="Fahrenheit" time-layout="k-p24h-n2-2">
        <name>Daily Minimum Temperature</name>
        <value>44</value>
        <value>52</value>
      </temperature>
      <temperature type="hourly" units="Fahrenheit" time-layout="k-p3h-n9-3">
        <name>Temperature</name>
        <value>48</value>
        <value>48</value>
        <value>49</value>
        <value>57</value>
        <value>63</value>
        <value>62</value>
        <value>58</value>
        <value>56</value>
        <value>55</value>
      </temperature>
      <temperature type="dew point" units="Fahrenheit" time-layout="k-p3h-n9-3">
        <name>Dew Point Temperature</name>
        <value>40</value>
        <value>43</value>
        <value>45</value>
        <value>46</value>
        <value>47</value>
        <value>48</value>
        <value>49</value>
        <value>48</value>
        <value>47</value>
      </temperature>
      <wind-speed type="sustained" units="knots" time-layout="k-p3h-n9-3">
        <name>Wind Speed</name>
        <value>1</value>
        <value>1</value>
        <value>2</value>
        <value>4</value>
        <value>6</value>
        <value>3</value>
        <value>1</value>
        <value>1</value>
        <value>2</value>
      </wind-speed>
      <humidity type="relative" units="percent" time-layout="k-p3h-n9-3">
        <name>Relative Humidity</name>
        <value>74</value>
        <value>83</value>
        <value>86</value>
        <value>67</value>
        <value>56</value>
        <value>60</value>
        <value>72</value>
        <value>74</value>
        <value>74</value>
      </humidity>
    </parameters>
  </data>
</dwml>

現在、私は を使用してこの XML を解析してDocumentBuilderおり、このデータをかなり初歩的に出力しています。つまり、次のように解析しています。

 eElement.getElementsByTagName("temperature").item(2).getTextContent();

これは、タグ name temperature、3 番目の項目 ( item(2)) で要素を取得し、その方法でテキスト コンテンツを取得することです。ただし、属性を指定してコンテンツを取得することに興味があります。つまり、type.

を指定して、この XML データの内容を表示する方法はありtypeますか? typeasを指定し、hourlyすべての内容を の下に出力し<name>Temperature</name>ます。

それがどのように機能するかの疑似コードは次のとおりです。

if(attributeType()==hourly){
    print all values of hourly temperature
}

ありがとうございました

4

1 に答える 1

1

この種のものにはXPathを使用します。作業がずっと簡単です。

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(XML_FILE_URI);

XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/temperature[@type='hourly']/value/text()");

NodeList result = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < result.getLength(); i++) {
    System.out.println(result.item(i).getNodeValue());
}
于 2013-10-30T16:02:07.860 に答える