如何解析Android中xml文件中标记值的属性

问题描述:

我正在开发一个天气应用程序。 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

我觉得你的问题是因为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

你正试图从标签读取值。

据我所知,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