Skip to main content

Section 15.11 Write Code Questions

The file "books.xml" has information for the books in a bookstore.
Data: books.xml
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>

    <book category="cooking">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>

    <book category="children">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>

    <book category="web">
        <title lang="en">XQuery Kick Start</title>
        <author>James McGovern</author>
        <author>Per Bothner</author>
        <author>Kurt Cagle</author>
        <author>James Linn</author>
        <author>Vaidyanathan Nagarajan</author>
        <year>2003</year>
        <price>49.99</price>
    </book>

    <book category="web" cover="paperback">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
    </book>

</bookstore>

Activity 15.11.1.

Fix the errors in the code below so that it reads the data from books.xml and finds all of the book data and prints the title for each book and then finds all the author names for each book and prints each author name.
The file "news.xml" has information for news stories.
Data: news.xml
<?xml version="1.0" encoding="UTF-8"?>
<nitf>
    <head>
        <title>Colombia Earthquake</title>
    </head>
    <body>
        <headline>
            <hl1>143 Dead in Colombia Earthquake</hl1>
        </headline>
        <byline>
            <bytag>By Jared Kotler, Associated Press Writer</bytag>
        </byline>
        <dateline>
            <location>Bogota, Colombia</location>
            <date>Monday January 25 1999 7:28 ET</date>
        </dateline>
    </body>
</nitf>

Activity 15.11.2.

Fix the errors in the code below so that it reads the data from news.xml and prints the headline and date.
The following file "weather.xml" has information for the weather for a particular day and time at an airport.
Data: weather.xml
<?xml version="1.0" encoding="UTF-8"?>
<current_observation>

    <location>New York/John F. Kennedy Intl Airport, NY</location>
    <station_id>KJFK</station_id>
    <latitude>40.66</latitude>
    <longitude>-73.78</longitude>
    <observation_time_rfc822>Mon, 11 Feb 2008 06:51:00 -0500 EST</observation_time_rfc822>
    <weather>A Few Clouds</weather>
    <temp_f>11</temp_f>
    <temp_c>-12</temp_c>
    <relative_humidity>36</relative_humidity>
    <wind_dir>West</wind_dir>
    <wind_degrees>280</wind_degrees>
    <wind_mph>18.4</wind_mph>
    <wind_gust_mph>29</wind_gust_mph>
    <pressure_mb>1023.6</pressure_mb>
    <pressure_in>30.23</pressure_in>
    <dewpoint_f>-11</dewpoint_f>
    <dewpoint_c>-24</dewpoint_c>
    <windchill_f>-7</windchill_f>
    <windchill_c>-22</windchill_c>
    <visibility_mi>10.00</visibility_mi>

</current_observation>

Activity 15.11.3.

Finish the code below so that it reads the data from weather.xml into a tree and then prints the location, temp_f, and windchill_f for the current_observation.
The following file "email.json" has first name, last name, email, gender, and ip_address information for people.
Data: email.json
[{
  "id": 1,
  "first_name": "Jeanette",
  "last_name": "Penddreth",
  "email": "[email protected]",
  "gender": "Female",
  "ip_address": "26.58.193.2"
}, {
  "id": 2,
  "first_name": "Giavani",
  "last_name": "Frediani",
  "email": "[email protected]",
  "gender": "Male",
  "ip_address": "229.179.4.212"
}, {
  "id": 3,
  "first_name": "Noell",
  "last_name": "Bea",
  "email": "[email protected]",
  "gender": "Female",
  "ip_address": "180.66.162.255"
}, {
  "id": 4,
  "first_name": "Willard",
  "last_name": "Valek",
  "email": "[email protected]",
  "gender": "Male",
  "ip_address": "67.76.188.26"
}]

Activity 15.11.4.

Finish the code below so that it prints the first name, last name and email address for each person in the list of dictionaries returned from json.loads(data).
The following file "person.json" has first name, last name, a flag if they are alive, age, address, phone numbers, spouse, and a list of children information for people.
Data: person.json
{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 27,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "office",
      "number": "646 555-4567"
    },
    {
      "type": "mobile",
      "number": "123 456-7890"
    }
  ],
  "children": [],
  "spouse": null
}

Activity 15.11.5.

Finish the code below so that it prints the first name, last name, the state the person lives in, and their mobile phone number.
You have attempted of activities on this page.