15.11. Write Code QuestionsΒΆ
Click on the button below to see the contents of books.xml. It contains xml tags to define the books in a bookstore.
<?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>
Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003 49.99 Learning XML Erik T. Ray 2003 39.95
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.
Click on the button below to see the contents of news.xml. It contains xml tags to define news stories.
<?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>
Colombia Earthquake 143 Dead in Colombia Earthquake By Jared Kotler, Associated Press Writer Bogota, Colombia Monday January 25 1999 7:28 ET
Fix the errors in the code below so that it reads the data from news.xml and prints the headline and date.
Click on the button below to see the contents of weather.xml. It contains xml tags to define weather observations.
<?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>
NOAA's National Weather Service http://weather.gov/ http://weather.gov/images/xml_logo.gif NOAA's National Weather Service http://weather.govNew York/John F. Kennedy Intl Airport, NY KJFK 40.66 -73.78 Mon, 11 Feb 2008 06:51:00 -0500 EST A Few Clouds 11 -12 36 West 280 18.4 29 1023.6 30.23 -11 -24 -7 -22 10.00 http://weather.gov/weather/images/fcicons/ nfew.jpg http://weather.gov/disclaimer.html http://weather.gov/disclaimer.html
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 file email.json
below contains JSON data for people including their first_name, last_name
and email address.
[{ "id": 1, "first_name": "Jeanette", "last_name": "Penddreth", "email": "jpenddreth0@census.gov", "gender": "Female", "ip_address": "26.58.193.2" }, { "id": 2, "first_name": "Giavani", "last_name": "Frediani", "email": "gfrediani1@senate.gov", "gender": "Male", "ip_address": "229.179.4.212" }, { "id": 3, "first_name": "Noell", "last_name": "Bea", "email": "nbea2@imageshack.us", "gender": "Female", "ip_address": "180.66.162.255" }, { "id": 4, "first_name": "Willard", "last_name": "Valek", "email": "wvalek3@vk.com", "gender": "Male", "ip_address": "67.76.188.26" }]
[{ "id": 1, "first_name": "Jeanette", "last_name": "Penddreth", "email": "jpenddreth0@census.gov", "gender": "Female", "ip_address": "26.58.193.2" }, { "id": 2, "first_name": "Giavani", "last_name": "Frediani", "email": "gfrediani1@senate.gov", "gender": "Male", "ip_address": "229.179.4.212" }, { "id": 3, "first_name": "Noell", "last_name": "Bea", "email": "nbea2@imageshack.us", "gender": "Female", "ip_address": "180.66.162.255" }, { "id": 4, "first_name": "Willard", "last_name": "Valek", "email": "wvalek3@vk.com", "gender": "Male", "ip_address": "67.76.188.26" }]
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 file person3.json
below contains JSON data for a person in a dictionary including their first name,
last name, address, and phone numbers.
{ "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 }
{ "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": "mobile", "number": "123 456-7890" } ], "children": [], "spouse": null }
Finish the code below so that it prints the first name, last name, the
state the person lives in, and their mobile phone number from the JSON loaded from person3.json
.