【问题标题】:How to parse properties of a tag value in xml file in Android如何在Android中解析xml文件中标签值的属性
【发布时间】:2014-09-03 11:25:37
【问题描述】:

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

【问题讨论】:

    标签: android xml-parsing saxparser xml-attribute


    【解决方案1】:

    我认为你的问题是因为 xml 文件使用了命名空间。而且您无法从yweather 命名空间中读取。

    为此我会使用XmlPullParser(我最喜欢它)

    首先您要指定 Feature 并设置一个 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();
    }
    

    【讨论】:

    • 感谢您的评论。
    【解决方案2】:

    您正在尝试从标签中读取值。

    据我所知,SAXParser 读取整个标签并返回这些标签之间的值,因为我已经这样做了,现在我试图在 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>
    

    因为开始元素搜索(您可以指定自己的)和结束元素搜索标签之间的所有标签,直到找不到....您可以根据您的要求跳过任何标签

    或者试试这个可能对你有帮助

    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");
    

    【讨论】:

    • 感谢您的评论。 Xml PullParser 为我工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多