Isuru
Isuru

Reputation: 161

How to parse properties of a tag value in xml file in Android

I'm developing a weather application. The Xml file is successfully parsed. But I want to read this value.

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

But when I get astronomy to a text feild, it returns null. But In logcat it is shows that astronomy tag has been passed.

I want to get the values of sunrise and sunset. Please help me with this. Thanks in advance

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(); 
                }   
        } 

This is the xml file http://weather.yahooapis.com/forecastrss?w=2189713

Upvotes: 0

Views: 1785

Answers (2)

pajus_cz
pajus_cz

Reputation: 1321

I think your problem is caused because the xml file is using namespaces. And you can not read from yweather namespace.

For this I would use XmlPullParser (I like it the most)

first you muse specify Feature and set an InputStream from which you will read the xml file.

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

Then you need to parse the entire document something like:

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();
}

Upvotes: 2

Mustanser Iqbal
Mustanser Iqbal

Reputation: 5097

You are trying to read values from tag.

According to my knowledge SAXParser reads the Whole tag and returns value between these tags because i have already done this and now m trying to place data after a specific tag in XML file but fail every time help me if you can.

 <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 start element search (you can specify your own) and end element search all the tags between tags untill <\Placemark> is not found.... you can skip any tag according to your requirment

OR Try this may it help you

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

Upvotes: 1

Related Questions