I am a beginner and am having a lot of difficulties with parsing an XML from a website.
Here is the code to parse the XML after it has been downloaded into a file:
public RSSFeed readFile() throws FileNotFoundException { RSSFeed feed = new RSSFeed(); RSSLocation location = new RSSLocation(); RSSTime time = new RSSTime(); XmlPullParser parser = Xml.newPullParser(); InputStream inputStream = context.openFileInput(FILENAME); try { parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(inputStream,null); int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if(eventType == XmlPullParser.START_TAG){ // yweather:forecast - forecast is name of the element, yweather is namespace Log.d("Parser Name", parser.getName()); Log.d("Parser Text", parser.getText()); Log.d("Parser Desc", parser.getPositionDescription()); } eventType = parser.next(); } } catch (Exception e) { Log.e("XML Parser", e.toString()); } return feed; } Here is the XML file: http://api.openweathermap.org/data/2.5/forecast?lat=40.2845939&lon=-75.6475172&mode=xml&cnt=3&appid=2de143494c0b295cca9337e1e96b00e0
I am simply trying to see how the data is being parsed so I can then appropriately assign the objects and their attributes.
I just want to see in the log what is returned based on each method (getName(), getText(), getAttributeValue()...)
However, when I use the above code to send the information to the log, it is only getting the first tag <weatherdata> and returning a NullPointerException: println needs a message for the Text.
Here is the logcat:
12-06 14:52:30.009 17507-17557/com.example.shortn1.whattowear E/XML Parser: java.lang.NullPointerException: println needs a message 12-06 14:52:30.049 17507-17507/com.example.shortn1.whattowear D/Weather Feed: Feed displayed 12-06 14:52:50.849 17507-17554/com.example.shortn1.whattowear D/Parser Name: weatherdata 12-06 14:52:50.849 17507-17554/com.example.shortn1.whattowear E/XML Parser: java.lang.NullPointerException: println needs a message 12-06 14:52:50.889 17507-17507/com.example.shortn1.whattowear D/Weather Feed: Feed displayed 12-06 14:53:10.874 17507-17856/com.example.shortn1.whattowear D/Parser Name: weatherdata 12-06 14:53:10.874 17507-17856/com.example.shortn1.whattowear E/XML Parser: java.lang.NullPointerException: println needs a message 12-06 14:53:10.919 17507-17507/com.example.shortn1.whattowear D/Weather Feed: Feed displayed 12-06 14:53:30.809 17507-17557/com.example.shortn1.whattowear D/Parser Name: weatherdata 12-06 14:53:30.809 17507-17557/com.example.shortn1.whattowear E/XML Parser: java.lang.NullPointerException: println needs a message 12-06 14:53:30.839 17507-17507/com.example.shortn1.whattowear D/Weather Feed: Feed displayed My main activity is too long to post here, but there is a location finder and in onLocationChanged() the method is called to download the xml corresponding to the person's location. Once the file is returned, the readFile() method is called. After the file is read, another method is called (which is logging "Feed displayed").
I also tried changing the XML Parser to only grab the "name" field from the XML by adding && parser.getName().equals("name") but it still only reads the word 'name' and does not return the value of the name.
Any suggestions? All help is appreciated!! Thank you ahead.
No comments:
Post a Comment