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

标签 java android xml api

我有一个 xml 数据,它是一个 BufferedReader,其中包含 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>

我想提取一些值,例如- 日出=“2016-05-28T10:08:49” 将sunset =“2016-05-29T00:53:59”预测节点放入变量中。这样我就可以过滤到我的类变量的预测值。如果需要任何说明,请告诉我。 欢迎任何想法。

最佳答案

您可以尝试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

关于java - 在 Java 中过滤 xml 数据的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37588524/

相关文章:

regex - Sed:仅当标记与第一个字符串匹配时才转换为 xml 标记

Java递归性的误解

javascript - 我在react-native中获取了带有fetch的JSON,如何将结果发送到其他组件

java - 警报对话框的按钮被切断

python 和XML : how to place two documents into a single document

python - 如何使用python获取xml文件中的部分内容?

java - opencv编辑图像使文字更清晰

java - Gradle 错误 :Gradle DSL method not found: 'executeWithoutThrowingTaskFailure()'

java - MysqlDump 在空格分隔的路径中不起作用

android - 使用默认的android图像查看器显示图片