Modify the program in Fig. 16.13. Allow the user to add a new element to each element. For instance, if the user adds a phoneNumber element, the user should be prompt- ed to provide a phone number for each contact. Each time a user adds a contact, the user should be prompted to provide information for any new elements in addition to the first and last names. Func- tion printList
should print any new information as well as the contact’s first and last names.
What will be an ideal response?
```
# Using 4DOM to manipulate an XML Document.
import sys
from xml.dom.ext.reader import PyExpat
from xml.dom.ext import PrettyPrint
import sys
from xml.dom.ext.reader import PyExpat
from xml.dom.ext import PrettyPrint
newElements = [] # list of new elements
def printInstructions():
print """\nEnter 'a' to add a contact.
Enter 'l' to list contacts.xml.
Enter 'i' for instructions.
Enter 'e' to create an element.
Enter 'q' to quit."""
def printList( document ):
print "Your contact list is:"
# iterate over list of
for contact in document.getElementsByTagName( "contact" ):
first = contact.getElementsByTagName( "FirstName" )[ 0 ]
firstText = first.firstChild.nodeValue
print firstText,
last = contact.getElementsByTagName( "LastName" )[ 0 ]
lastText = last.firstChild.nodeValue
print lastText
# iterate over added elements
for element in newElements:
elementName = contact.getElementsByTagName( element )[ 0 ]
elementText = elementName.firstChild.nodeValue
print element + ": " + elementText
print
def addContact( document ):
root = document.documentElement
name = raw_input(
"Enter the name of the person you wish to add: " )
first, last = name.split()
# create first name node
firstNode = document.createElement( "FirstName" )
firstNodeText = document.createTextNode( first )
firstNode.appendChild( firstNodeText )
# create last name node
lastNode = document.createElement( "LastName" )
lastNodeText = document.createTextNode( last )
lastNode.appendChild( lastNodeText )
# create contact node, append first name and last name nodes
contactNode = document.createElement( "contact" )
contactNode.appendChild( firstNode )
contactNode.appendChild( lastNode )
# prompt user to enter information for each added node
for element in newElements:
text = raw_input( "Enter " + element + ": " )
newNode = document.createElement( element )
newNodeText = document.createTextNode( text )
newNode.appendChild( newNodeText )
contactNode.appendChild( newNode )
root.appendChild( contactNode ) # add contact node
# allows user to add new element to each
def addElement( document ):
root = document.documentElement
element = raw_input( "Enter new element: " )
newElements.append( element )
# prompts user to enter information for each
for contact in document.getElementsByTagName( "contact" ):
first = contact.getElementsByTagName( "FirstName" )[ 0 ]
firstText = first.firstChild.nodeValue
last = contact.getElementsByTagName( "LastName" )[ 0 ]
lastText = last.firstChild.nodeValue
text = raw_input(
"Enter the " + element + " for " + lastText +
", " + firstText + ": " )
newNode = document.createElement( element )
newNodeText = document.createTextNode( text )
newNode.appendChild( newNodeText )
contact.appendChild( newNode )
# open contacts file
try:
file = open( "contacts.xml", "r+" )
except IOError:
sys.exit( "Error opening file" )
# create DOM
reader = PyExpat.Reader()
document = reader.fromStream( file )
printList( document )
printInstructions()
character = "l"
while character != "q":
character = raw_input( "\n? " )
if character == "a":
addContact( document )
elif character == "l":
printList( document )
elif character == "i":
printInstructions()
elif character == "e":
addElement( document )
elif character != "q":
print "Invalid command!"
file.seek( 0, 0 ) # go to beginning of file
file.truncate() # remove data from file
PrettyPrint( document, file ) # print DOM contents to file
file.close() # close XML file
reader.releaseNode( document ) # free memory
```
Your contact list is:
John Black
Sue Green
Enter 'a' to add a contact.
Enter 'l' to list contacts.xml.
Enter 'i' for instructions.
Enter 'e' to create an element.
Enter 'q' to quit.
? e
Enter new element: phoneNumber
Enter the phonenumber for Black, John: 555-1234
Enter the phonenumber for Green, Sue: 555-5678
? a
Enter the name of the person you wish to add: Mary Blue
Enter phonenumber: 555-8888
? l
Your contact list is:
John Black
phonenumber: 555-1234
Sue Green
phonenumber: 555-5678
Mary Blue
phonenumber: 555-8888
You might also like to view...
Using ____, you can connect a cable between the computer and your digital camera to transfer photos.
A. FireWire ports B. infrared ports C. a docking station D. a card reader
You can fill an individual cell with a picture
Indicate whether the statement is true or false
Progressive scaling refreshes all the horizontal lines simultaneously
Indicate whether the statement is true or false
______ is the file manager included in OS X
A) Dock B. Finder C. Menu bar D. Explore