简体   繁体   中英

Printing XML elements by specifying its attribute

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

Currently I am parsing this XML using DocumentBuilder , and I am printing this data rather rudimentarily, that is, I am parsing it like this:

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

What that does is I am getting the element by the tag name temperature , the third item ( item(2) ), and getting the text content that way. However, I am interested in getting the content by specifying the attribute, and that is, through the type .

Is there anyway to display the content of this XML data through specifying the type ? By specifying the type as hourly , and printing all contents under <name>Temperature</name> .

A psuedocode of how that would work is:

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

Thank you

I'd use XPath for this sort of thing. It's just so much easier to work with.

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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM