在Java中过滤xml数据的更好方法

时间:2016-06-02 09:56:59

标签: java android xml api

我有一个BufferedReader的xml数据,其中包含xml格式的天气数据,如

<weatherdata>
    <location>
        <name>London</name>
        <type/>
        <country>US</country>
        <timezone/>
        <location altitude="0" latitude="39.886452" longitude="-83.44825" geobase="geonames" geobaseid="0"/>
    </location>
    <credit/>
    <meta>
        <lastupdate/>
        <calctime>0.0188</calctime>
        <nextupdate/>
    </meta>
    <sun rise="2016-05-28T10:08:49" set="2016-05-29T00:53:59"/>
    <forecast>
        <time from="2016-05-28T09:00:00" to="2016-05-28T12:00:00">
            <symbol number="802" name="scattered clouds" var="03d"/>
            <precipitation/>
            <windDirection deg="199" code="SSW" name="South-southwest"/>
            <windSpeed mps="3.91" name="Gentle Breeze"/>
            <temperature unit="celsius" value="21.88" min="21.88" max="22.39"/>
            <pressure unit="hPa" value="996.94"/>
            <humidity value="79" unit="%"/>
            <clouds value="scattered clouds" all="32" unit="%"/>
        </time>
        <time from="2016-05-28T12:00:00" to="2016-05-28T15:00:00">
            <symbol number="802" name="scattered clouds" var="03d"/>
            <precipitation/>
            <windDirection deg="201.505" code="SSW" name="South-southwest"/>
            <windSpeed mps="4.61" name="Gentle Breeze"/>
            <temperature unit="celsius" value="25.89" min="25.89" max="26.27"/>
            <pressure unit="hPa" value="997.77"/>
            <humidity value="78" unit="%"/>
            <clouds value="scattered clouds" all="36" unit="%"/>
        </time>
        <time from="2016-05-28T15:00:00" to="2016-05-28T18:00:00">
            <symbol number="801" name="few clouds" var="02d"/>
            <precipitation/>
            <windDirection deg="208" code="SSW" name="South-southwest"/>
            <windSpeed mps="4.56" name="Gentle Breeze"/>
            <temperature unit="celsius" value="28.1" min="28.1" max="28.35"/>
            <pressure unit="hPa" value="997.3"/>
            <humidity value="73" unit="%"/>
            <clouds value="few clouds" all="24" unit="%"/>
        </time>
        <time from="2016-05-28T18:00:00" to="2016-05-28T21:00:00">
            <symbol number="802" name="scattered clouds" var="03d"/>
            <precipitation/>
            <windDirection deg="201.008" code="SSW" name="South-southwest"/>
            <windSpeed mps="5.01" name="Gentle Breeze"/>
            <temperature unit="celsius" value="28.1" min="28.1" max="28.23"/>
            <pressure unit="hPa" value="996.51"/>
            <humidity value="66" unit="%"/>
            <clouds value="scattered clouds" all="32" unit="%"/>
        </time>
        <time from="2016-05-28T21:00:00" to="2016-05-29T00:00:00">
            <symbol number="500" name="light rain" var="10n"/>
            <precipitation unit="3h" value="0.45" type="rain"/>
            <windDirection deg="187.001" code="S" name="South"/>
            <windSpeed mps="4.38" name="Gentle Breeze"/>
            <temperature unit="celsius" value="26.43" min="26.43" max="26.43"/>
            <pressure unit="hPa" value="996.59"/>
            <humidity value="63" unit="%"/>
            <clouds value="broken clouds" all="64" unit="%"/>
        </time>
        <time from="2016-05-29T00:00:00" to="2016-05-29T03:00:00">
            <symbol number="500" name="light rain" var="10n"/>
            <precipitation unit="3h" value="0.63" type="rain"/>
            <windDirection deg="217" code="SW" name="Southwest"/>
            <windSpeed mps="3.46" name="Gentle Breeze"/>
            <temperature unit="celsius" value="24.14" min="24.14" max="24.14"/>
            <pressure unit="hPa" value="997.53"/>
            <humidity value="67" unit="%"/>
            <clouds value="broken clouds" all="80" unit="%"/>
        </time>
        <time from="2016-05-29T03:00:00" to="2016-05-29T06:00:00">
            <symbol number="803" name="broken clouds" var="04n"/>
            <precipitation/>
            <windDirection deg="242.504" code="WSW" name="West-southwest"/>
            <windSpeed mps="3.81" name="Gentle Breeze"/>
            <temperature unit="celsius" value="22.2" min="22.2" max="22.2"/>
            <pressure unit="hPa" value="997.86"/>
            <humidity value="69" unit="%"/>
            <clouds value="broken clouds" all="76" unit="%"/>
        </time>
        <time from="2016-05-29T06:00:00" to="2016-05-29T09:00:00">
            <symbol number="803" name="broken clouds" var="04n"/>
            <precipitation/>
            <windDirection deg="237.501" code="WSW" name="West-southwest"/>
            <windSpeed mps="3.46" name="Gentle Breeze"/>
            <temperature unit="celsius" value="21.06" min="21.06" max="21.06"/>
            <pressure unit="hPa" value="998"/>
            <humidity value="73" unit="%"/>
            <clouds value="broken clouds" all="76" unit="%"/>
        </time> 
    </forecast>
</weatherdata>

我想提取一些像 - sunrise =&#34; 2016-05-28T10:08:49&#34; 日落=&#34; 2016-05-29T00:53:59&#34;将节点预测为变量。这样我就可以对我的类变量进行预测值。如果需要澄清,请告诉我。 任何想法都是受欢迎的。

2 个答案:

答案 0 :(得分:1)

您可以尝试jcabi-xml,它非常易于使用,您可以使用XPath获取下一个属性:

XML xml = new XMLDocument(new StreamSource(reader));
System.out.printf(
    "rise=%s, set=%s%n",
    xml.xpath("/weatherdata/sun/@rise").get(0),
    xml.xpath("/weatherdata/sun/@set").get(0)
);

<强>输出:

rise=2016-05-28T10:08:49, set=2016-05-29T00:53:59

答案 1 :(得分:0)

您可以尝试使用 XmlPullParser

here 就是一个例子。

更多信息:XmlPullParser