Hi everyone, i was given a book.xml file which contains all the title,author of the books.

Here's how it looks like.

<?xml version="1.0" ?>
<Books>
<Item>
<BookID>A001</BookID>
<Title>A Tale of Two Cities</Title>
<Author>Charles Dickens</Author>
<Language>English</Language>
<Year>1859</Year>
<Quantity>5</Quantity>
</Item>
<Item>
<BookID>A002</BookID>
<Title>The Lord of the Rings</Title>
<Author>J. R. R. Tolkien</Author>
<Language>English</Language>
<Year>1954</Year>
<Quantity>5</Quantity>
</Item>
<Item>
<BookID>A003</BookID>
<Title>The Hobbit</Title>
<Author>J. R. R. Tolkien</Author>
<Language>English</Language>
<Year>1937</Year>
<Quantity>4</Quantity>
</Item>


Can someone here kindly explain to me how am i suppose to start with to achieve the result below?


[IMG]http://i179.photobucket.com/albums/w281/ZzbBoyzZ/Capture.jpg[/IMG]

As tony postet you have to use a paser.
lxml is good,for me BeautifulSoup has alway been a favorit.

lxml is more upp date and has more features.
But BeautifulSoup is just one small file(easy to disbrute),and still dos a very good jobb.

The advantages with lxml and BeautifulSoup is that you can parse xml/html that is not correct written.
All xml module in standar library,will shut down on malformed xml/html.
Here just a demo with BeautifulSoup.

from BeautifulSoup import BeautifulStoneSoup

xml = '''\
<?xml version="1.0" ?>
<Books>
<Item>
<BookID>A001</BookID>
<Title>A Tale of Two Cities</Title>
<Author>Charles Dickens</Author>
<Language>English</Language>
<Year>1859</Year>
<Quantity>5</Quantity>
</Item>
<Item>
<BookID>A002</BookID>
<Title>The Lord of the Rings</Title>'''


soup = BeautifulStoneSoup(xml)
tag = soup.findAll('title')
for title in tag:
    print title.string

'''Output-->
A Tale of Two Cities
The Lord of the Rings
'''

Can someone correct me on my code below??? How am i suppose to display the book titles published in 1954?

1) Display all the book titles
2) Display the book titles published in 1954
3) Display the 3 most recently published book titles
4) Display the author of “The Hobbit”

<?xml version="1.0" ?>
<Books>
<Item>
<BookID>A001</BookID>
<Title>A Tale of Two Cities</Title>
<Author>Charles Dickens</Author>
<Language>English</Language>
<Year>1859</Year>
<Quantity>5</Quantity>
</Item>
<Item>
<BookID>A002</BookID>
<Title>The Lord of the Rings</Title>
<Author>J. R. R. Tolkien</Author>
<Language>English</Language>
<Year>1954</Year>
<Quantity>5</Quantity>
</Item>
<Item>
<BookID>A003</BookID>
<Title>The Hobbit</Title>
<Author>J. R. R. Tolkien</Author>
<Language>English</Language>
<Year>1937</Year>
<Quantity>4</Quantity>
</Item>
<Item>
</Books>

<?xml version="1.0" ?>
<Patrons>
<Item>
<PatronID>U001</PatronID>
<Name>Peter</Name>
<Gender>Male</Gender>
<Age>20</Age>
<BorrowedBookID>A001</BorrowedBookID>
</Item>
<Item>
<PatronID>U002</PatronID>
<Name>Mary</Name>
<Gender>Female</Gender>
<Age>18</Age>
<BorrowedBookID>None</BorrowedBookID>
</Item>
<Item>
</Patrons>


My code:

import xml.dom.minidom
import xpath


books = open('Books.xml')
AA = books.read()

patron = open('Patron.xml')
BB = patron.read()

xml_book = xml.dom.minidom.parseString(AA)
xml_patron = xml.dom.minidom.parseString(BB)
patron.close()
books.close()

def option():

print "1) Display all the book titles"
print "2) Display the book titles published in 1950"
print "3) Display the 3 most recently published book titles"
print "4) Display the author of The Hobbit"
print "5) Quit"

option()
x= int(raw_input ("Please enter your selection:"))


list1 = xpath.find('//Title',xml_book)
list2 = xpath.find('/books/item/year[1950]/title',xml_book)

while x !=0:
if x == 1:

for i in range(len(list1)):

title = list1.firstChild.nodeValue

print title

print ''
option()
x= int(raw_input ("Please enter your selection:"))

elif x == 2:
for i in range(len(list2)):
if xpath.find('(/books/item/year)',xml_book) == '1950':

print xpath.find('(/item/year)',xml_book)


print ''
option()
x= int(raw_input ("Please enter your selection:"))

Can someone correct me on my code below??? How am i suppose to display the book titles published in 1954?

1) Display all the book titles
2) Display the book titles published in 1954
3) Display the 3 most recently published book titles
4) Display the author of “The Hobbit”

<?xml version="1.0" ?>
<Books>
<Item>
<BookID>A001</BookID>
<Title>A Tale of Two Cities</Title>
<Author>Charles Dickens</Author>
<Language>English</Language>
<Year>1859</Year>
<Quantity>5</Quantity>
</Item>
<Item>
<BookID>A002</BookID>
<Title>The Lord of the Rings</Title>
<Author>J. R. R. Tolkien</Author>
<Language>English</Language>
<Year>1954</Year>
<Quantity>5</Quantity>
</Item>
<Item>
<BookID>A003</BookID>
<Title>The Hobbit</Title>
<Author>J. R. R. Tolkien</Author>
<Language>English</Language>
<Year>1937</Year>
<Quantity>4</Quantity>
</Item>
<Item>
</Books>

<?xml version="1.0" ?>
<Patrons>
<Item>
<PatronID>U001</PatronID>
<Name>Peter</Name>
<Gender>Male</Gender>
<Age>20</Age>
<BorrowedBookID>A001</BorrowedBookID>
</Item>
<Item>
<PatronID>U002</PatronID>
<Name>Mary</Name>
<Gender>Female</Gender>
<Age>18</Age>
<BorrowedBookID>None</BorrowedBookID>
</Item>
<Item>
</Patrons>


My code:

import xml.dom.minidom
import xpath


books = open('Books.xml')
AA = books.read()

patron = open('Patron.xml')
BB = patron.read()

xml_book = xml.dom.minidom.parseString(AA)
xml_patron = xml.dom.minidom.parseString(BB)
patron.close()
books.close()

def option():

print "1) Display all the book titles"
print "2) Display the book titles published in 1950"
print "3) Display the 3 most recently published book titles"
print "4) Display the author of The Hobbit"
print "5) Quit"

option()
x= int(raw_input ("Please enter your selection:"))


list1 = xpath.find('//Title',xml_book)
list2 = xpath.find('/books/item/year[1950]/title',xml_book)

while x !=0:
if x == 1:

for i in range(len(list1)):

title = list1.firstChild.nodeValue

print title

print ''
option()
x= int(raw_input ("Please enter your selection:"))

elif x == 2:
for i in range(len(list2)):
if xpath.find('(/books/item/year)',xml_book) == '1950':

print xpath.find('(/item/year)',xml_book)


print ''
option()
x= int(raw_input ("Please enter your selection:"))

Please use code tags

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.