0% found this document useful (0 votes)
50 views

Exercise 3: Create An XML File: Step 1: One Day's Forecast

The document provides instructions for creating an XML file to capture weather forecast data over multiple days. It describes creating a single-day forecast with elements for date, description, maximum and minimum temperatures, wind speed, and danger level. It then explains how to expand this into a three-day forecast within a top-level <forecast> element, including modifying values for a second day and leaving the third day empty.

Uploaded by

Sanjay Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Exercise 3: Create An XML File: Step 1: One Day's Forecast

The document provides instructions for creating an XML file to capture weather forecast data over multiple days. It describes creating a single-day forecast with elements for date, description, maximum and minimum temperatures, wind speed, and danger level. It then explains how to expand this into a three-day forecast within a top-level <forecast> element, including modifying values for a second day and leaving the third day empty.

Uploaded by

Sanjay Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Exercise 3: Create an XML file

In this exercise, you will create an XML file. Refer to Lecture 3 for tools that you can use to do
this. For this exercise, you will create XML that captures weather forecast data, much like what
you documented in Exercise 2 for JSON.

Step 1: One day's forecast.


First, you’ll need to create an object for one day’s forecast. The very first line needs to indicate
that the file is XML. Use this for your first line:

<?xml version="1.0" ?>

The top level element will be called dailyForecast, so let's create an opening and ending tag for
it:

<?xml version="1.0" ?>


<dailyForecast>

</dailyForecast>

Inside the dailyForecast element, let's add an element called "date". Its value will be
2015-06-01.

<?xml version="1.0" ?>


<dailyForecast>
<date>2015-06-01</date>
</dailyForecast>

Next, add a description element with content of "sunny". It will look like this:

<?xml version="1.0" ?>


<dailyForecast>
<date>2015-06-01</date>
<description>sunny</description>
</dailyForecast>

Now, add a maxTemp element for the high temperature forecast for that day. We can use an
attribute called "unit" with a value of "C" to say that it's in degrees Celsius. Have the maximum
temperature be 22.

<?xml version="1.0" ?>


<dailyForecast>
<date>2015-06-01</date>
<description>sunny</description>
<maxTemp unit="C">22</maxTemp>
</dailyForecast>
Now add a "minTemp" element with a unit attribute of "C" and content of 20, and a "windSpeed"
element with a unit attribute of "kph" and a value of 12. (This will indicate that the wind speed is
in kilometers per hour.) Finally, add an element called "danger" with content of false. (You can
imagine that software will check this value, and if danger is true, then it will pop up a special
alert.)

Take your finished XML and paste it into a XML formatter at:

http://www.freeformatter.com/xml-formatter.html.

This will validate that the XML is the correct syntax and if it is, the it will show you what your
XML will look like when nicely formatted.

Finally, compare your XML to what I came up with at: http://sdkbridge.com/ud/oneday.xml. Note
that your browser may format the XML and not show you the very first line.

Step 2: Three-day forecast


The next step is to create XML for a three-day forecast. Create a new file with the same first
line, and then a top level element called "forecast". It should look like this:

<?xml version="1.0" ?>


<forecast>

</forecast>

Copy and paste your Monday forecast into the array. Indent it properly (either with the XML
formatter, or by hand.)

<?xml version="1.0" ?>


<forecast>
<dailyForecast>
...
</dailyForecast>
</forecast>

(Where the … is, you will have the rest of your Step 1 XML file. But not the first line, since you
already have that one.)
Now, copy the dailyForecast element and paste it below so that you have a second one.

<?xml version="1.0" ?>


<forecast>
<dailyForecast>
...
</dailyForecast>
<dailyForecast>
...
</dailyForecast>
</forecast>

Change the values on the second element so that the date is 2015-06-02, the description is
"windy", the wind speed is 40, and danger is true.

Finally, add a third object in the array for the next day. But let's say that we were unable to get
the forecast, so the element is empty. See if you can remember how to do the shortcut that
shows the element is empty without having to use an end tag.

Again, validate and format your XML (http://www.freeformatter.com/xml-formatter.html).

Finally (and don't peek until you've really tried to figure it out), compare it to mine:

http://sdkbridge.com/ud/threeday.xml

Again, your browser may format the XML and not show you the very first line.

You might also like