简体   繁体   English

无法使用DOM解析XML结果中的元素

[英]Can't parse element from XML result using DOM

I am using Google weather api service. 我正在使用Google Weather API服务。 I am using DOM. 我正在使用DOM。 I have difficulty to get the element value. 我很难获得元素值。

thats an example of xml response: 多数民众赞成在xml响应的示例:

<xml_api_reply version="1">
 <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">  
     <forecast_information>
         <city data="New York, NY"/>
         <postal_code data="new york,ny"/>
         <latitude_e6 data=""/>
         <longitude_e6 data=""/>
         <forecast_date data="2010-05-20"/>
         <current_date_time data="2010-05-20 07:44:43 +0000"/>
         <unit_system data="US"/>
     </forecast_information>
    <current_conditions>
        <condition data="Cloudy"/>
        <temp_f data="59"/>
        <temp_c data="15"/>
        <humidity data="Humidity: 80%"/>
        <icon data="/ig/images/weather/cloudy.gif"/>
        <wind_condition data="Wind: N at 0 mph"/>
    </current_conditions>
    <forecast_conditions>
        <day_of_week data="Thu"/>
        <low data="61"/>
        <high data="79"/>
        <icon data="/ig/images/weather/sunny.gif"/>
        <condition data="Sunny"/>
        </forecast_conditions>
    <forecast_conditions>
        <day_of_week data="Fri"/>
        <low data="60"/>
        <high data="83"/>
        <icon data="/ig/images/weather/partly_cloudy.gif"/>
        <condition data="Partly Cloudy"/>
    </forecast_conditions>
</weather>


Now let's say I want to retrieve the value of the condition data which is under the tag 现在假设我要检索标记下的条件数据的值

(in this example i am trying to get the value="Cloudy" (在此示例中,我试图获取值=“ Cloudy”

this is what I do: 这是我的工作:

 void buildForecasts(String raw) throws Exception
{
    DocumentBuilder builder = DocumentBuilderFactory.newInstance()
            .newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(raw)));

    NodeList temps = doc.getElementsByTagName("current_conditions");



    for (int i = 0; i < temps.getLength(); i++)
    {
        Element temp = (Element) temps.item(i);
        String temp1 =temp.getAttribute("condition")
    }

}

it doesnt realy work for me. 它对我来说真的不工作。 anyone has any idea? 有人有什么主意吗?

thanks, ray. 谢谢,雷。

The temps list contains elements with name "current_conditions" (ie the node <current_conditions> in the XML). temps列表包含名称为“ current_conditions”的元素(即XML中的节点<current_conditions> )。 You need to get the sub-element named "condition" (ie the node <condition data="Cloudy"/> in the XML), and then get its data attribute. 您需要获取名为“ condition”的子元素(即XML中的节点<condition data="Cloudy"/> ),然后获取其data属性。

for (int i = 0; i < temps.getLength(); i++)
{
    Element currentConditionsElement = (Element) temps.item(i);
    NodeList conditionList = currentConditionsElement.getElementsByTagName("condition");
    Element conditionElement = (Element) conditionList.item(0);
    String dataAttribute = conditionElement.getAttribute("data");
}

如果您使用的是API级别8(Android 2.2),则可以使用XPath简化操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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