Work with various XML in-memory classes so as to read and parse those documents.
Extend an application that I have created, which will parse U.S. treasury bond XML data.
You might want to reference the following Android developer pages:
The following contains the reference to the Node class: http://developer.android.com/reference/org/w3c/dom/Node.html
The following contains the reference to the NodeList class: http://developer.android.com/reference/org/w3c/dom/NodeList.html
The following contains the reference to the org.w3c.dom Document object: http://developer.android.com/reference/org/w3c/dom/Document.html
The following links are to the XML documents that you will use in the lab.
The following contains the NOAA page that we are using: http://forecast.weather.gov/MapClick.php?lat=39.3233&lon=-120.379&unit=0&lg=english&FcstType=dwml
The following contains the U.S. Treasury page: http://data.treasury.gov/feed.svc/DailyTreasuryLongTermRateData?$filter=month(QUOTE_DATE)%20eq%204%20and%20year(QUOTE_DATE)%20eq%202015
HANDS-0N ACTIVITY: Add XML Code to an Existing Activity
In this first part of the lab, you will extend the application that I have created (NetworkDemo1). Please download a current copy. I have modified it since the initial upload. The .zip file appears on the Lecture notes page for the class:
This version of the application contains a Java file named ParseXMLActivity.java. It already contains the code to create the background thread, download the XML and parse it.
In this activity, you will extend the code to display additional information from the NOAA document.
Open the file named ParseXMLActivity.java. Scroll to the end of the file. Note that the following code already exists to get the text for the Description element. There is only one element with the tag named so nlDescription.item(0) contains the node from the list. The getFirstChild() method of the Node object returns the text node. The getNodeValue() method gets the actual text.
The following image shows the a segment of the XML document with which you will work next:
The XML document is configured such that, there is one element named . It has an immediate child named , and then multiple elements that contain the worded forecast for each consecutive day.
Note that I have played used item(3) to get the proper node. There is an extra text node that contains no data if you examine the parsed document.
Now, try this on your own.
Write the code to get the next forecast (tommorrow’s forecast). I have already created a TextView in the activity named tvForecastTommorow.
Now, see if you can get the daily maximum and minimum temperature for the first daily forecast:
HANDS-0N ACTIVITY: Creating a new activity and background task.
In this part of the lab, you will create another activity that will display quotes from the US treasury department. This lab corresponds to your final assignment. I have provided the XML for in the lab. You will be processing the following URL:
Create a new activity named ParseTreasuryXMLActivity.java.
On the main activity (ActivityMain.xml), create a button that will display the new activity. As shown in the other buttons, pass the above URL to this new activity.
Now, create the code template for this new activity. The code what you write should mimic the code appearing on the page ParseXMLActivity.java. That is, you will need to create the following
Override the onCreate method. Use the Connectivity manager to test that the network is available. If it is, execute the background worker process that you will create in a moment.
Create a class that mimics the DownloadXMLAndParse method from the ParseXMLActivity.java file.
Create the background worker (doInBackground) and other procedures and procedures using the ParseXMLActivity.java file as an example.
Implement the onPostExecute method which will retrieve information from the XML document and display it into corresponding form widgets.
You should be able to copy the downloadURL and readIt methods verbatim from the ParseXML Activity.java file.
Read the following data points from the XML file and display them.
Find the local name of the elements named properties. Using the first element, display the elements named QUOTE_DATE, RATE_TYPE, and RATE members.
Now extend the program. There are several elements named properties. Each contains a quote for a specific date. Get the number of
elements. The number will be between 1 and the total number of elements. Also the user to select / enter this value. Based on the value selected, display the corresponding quotation values.