通过指定其属性来打印XML元素

时间:2013-10-30 15:46:55

标签: java 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">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>

目前我正在使用DocumentBuilder解析此XML,而且我正在打印这些数据,就像我正在解析它:

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

我做的是通过标记名temperature,第三项(item(2))获取元素,并以此方式获取文本内容。但是,我有兴趣通过指定属性来获取内容,即通过type

是否有通过指定type来显示此XML数据的内容?将type指定为hourly,然后在<name>Temperature</name>下打印所有内容。

如何使用的伪代码是:

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

谢谢

1 个答案:

答案 0 :(得分: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());
}