Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Parsing and Manipulating XML Data Using Python: Can someone please write the code I need to put into python that goes along with the following

Parsing and Manipulating XML Data Using Python: Can someone please write the code I need to put into python that goes along with the following instructions? Also please tell me what I need to name the python file I am creating.

XML is the basis for many interfaces and web services. Consequently, reading and manipulating XML data is a common task in software development. Description An online plant distributor has recently experience a shortage in its supply of Anemone plants such that the price has increased by 20%. Their plant catalog is maintained in an XML file and they need a Python utility to find the plant by name, read the current price, change it by the specified percentage, and update the file. Writing this utility is your assignment. Using Pythons ElementTree XML API, write a Python program to perform the following tasks below. Note that your programs execution syntax must be as follows:

python xmlparse.py plant_catalog.xml plantName percentChange

Using ElementTree, read in this assignments XML file plant_catalog.xml specified by a command line parameter as shown above. Find the plant by the name passed in as an argument on the command line (plantName above). Once found, read the current price and adjust it by the command line argument percentChange. Note that this value could be anything in the range of -90 < percentChange < 100. For example, if you run your script as follows:

python plant_catalog.xml "Greek Valerian" -20 with the original XML containing:

Greek Valerian

Polemonium caeruleum

Annual

Shade

4.36

071499

The resulting file should contain:

Greek Valerian

Polemonium caeruleum

Annual

Shade

3.48

071499

Note: You may reduce the precision of the calculation if you wish but it isnt required.

Hints: Since XML is just a text file, you could write the code to read all the data and the decode the XML information. However, I certainly dont recommend this approach. Instead, let Python do it for you! Using Pythons ElementTree module, parse the file into an in-memory representation of the XML data. Once parsed, the root (or starting place) is as simple as requesting it from the tree. Once you have the root, you can call methods to find what you are looking for and modify them appropriately. You'll want to "findall" the plants and, "for" each plant "in" the result, you'll want to compare the name with the name passed on the command line. If you find a match you'll apply the percentage change, save the result back to the tree. When you are done with the search you will "write" the tree back to a file. I suggest using a different file name or you will be having to re-download the original with each run. One note of caution, be sure to read about XML in the Distributed Systems text. From doing so and reviewing the data file you will not that there are no attributes in the XML file. Consequently, you do not need to use attribute methods when you attempt this assignment. The following code snippet will give you a good starting point:

# Calling arguments: plant_catalog.xml plantName percentChange

import xml.etree.ElementTree as ET

import sys

# input parameters

searchName = sys.argv[2]

percent = float(sys.argv[3])

# parse XML data file

tree = ET.parse(sys.argv[1])

root = tree.getroot()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions

Question

KEY QUESTION Refer to the table in question

Answered: 1 week ago