gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:54:39 24 4
gpt4 key购买 nike

<?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数据的内容?通过指定typehourly ,并打印 <name>Temperature</name> 下的所有内容.

其工作原理的伪代码是:

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

谢谢

最佳答案

我会使用 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());
}

关于java - 通过指定其属性来打印 XML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19687522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com