2014-09-03 115 views
0

我正在開發一個天氣應用程序。 Xml文件被成功解析。但我想讀這個值。如何解析Android中xml文件中標記值的屬性

<yweather:astronomy sunrise="6:03 am" sunset="6:17 pm"/>

但是,當我拿到天文,下至一個文本費爾德,則返回null。但在logcat中顯示天文標籤已通過。

我想要得到日出和日落的價值。請幫我解決一下這個。在此先感謝

XmlHelper.java 


    @Override 
     public void endElement(String uri, String localName, String qName) throws SAXException 
     { 
      currTag = false; 

      if(localName.equalsIgnoreCase("pubDate")) post.setDescription(currTagVal); 
      else if(localName.equalsIgnoreCase("lastBuildDate")) post.setLastBuildDate(currTagVal); 
      else if(localName.equalsIgnoreCase("yweather:location city")) post.setLocation(currTagVal); 
      else if(localName.equalsIgnoreCase("channel")) Yweather.add(post); 
     } 


     @Override 
     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 
      Log.i(TAG, "TAG: " + localName); 
      currTag = true; currTagVal = ""; // Whenever <post> element is encountered it will create new object of PostValue 
      if(localName.equals("channel")) 
      { 
       post = new WeatherValues(); 
      } 

     } 

MainActivity.java

@Override 
     protected void onPostExecute(Void result) 
     { 
      StringBuilder builder = new StringBuilder(); 
      for(WeatherValues post : helper.Yweather) { 

       builder.append(post.getLocation()); 

     } 
       tvResponse.setText(builder.toString()); 
       pd.dismiss(); 
       } 
     } 

這是xml文件 http://weather.yahooapis.com/forecastrss?w=2189713

回答

1

我覺得你的問題是因爲XML文件使用的命名空間而引起的。而且你不能從yweather命名空間讀取。

爲此,我會用XmlPullParser(我喜歡它最)

首先你沉思指定功能和設置InputStream從中將讀取XML文件。

XmlPullParser parser = Xml.newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(Inputstream_from_which_you_read, null);

然後,你需要解析整個文件是這樣的:

int eventType = parser.getEventType(); 
while (eventType != XmlPullParser.END_DOCUMENT) { 
    if(eventType == XmlPullParser.START_TAG && parser.getName().equals("astronomy"){ 
    // yweather:forecast - forecast is name of the element, yweather is namespace 
     String attribute = parser.getAttributealue("yweather","sunrise"); // where you specify the namespace and attribute name 
    } 
    eventType = parser.next(); 
} 
+0

感謝您的評論。 – Isuru 2014-09-04 03:54:37

+0

歡迎您。希望它有幫助;) – 2014-09-05 19:32:49

1

你正試圖從標籤讀取值。

據我所知,SAXParser讀取整個標籤,並返回這些標籤之間的值,因爲我已經完成了這一步,現在m試圖將數據放在XML文件中的特定標籤之後,但如果可以的話,每次都會幫助我。

<Placemark id="2"> 
       <styleUrl>#icon-503-DB4436</styleUrl> 
       <name>Point 2</name> 
       <ExtendedData> 
       </ExtendedData> 
       <description><![CDATA[jc]]></description> 
       <Point> 
        <coordinates>73.07473,33.668113,0.0</coordinates> 
       </Point> 
      </Placemark> 

bcause開始元素搜索(可以指定你自己的)和結束元素搜索標記之間的所有標記,直到< \標>找不到....您可以根據您的需求量的跳過任何標籤

或嘗試,這可能是幫助你

String filepath = "c:\\file.xml"; 
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); 
    Document doc = docBuilder.parse(filepath); 

    Node placemark = doc.getElementsByTagName("Placemark").item(0); 
    NamedNodeMap attr = Placemark.getAttributes(); 
    Node nodeAttr = attr.getNamedItem("id"); 
+0

感謝您的評論。 Xml PullParser爲我工作。 – Isuru 2014-09-04 03:55:05