Python XML processing (newbie question)
Jay
jranchordas at hotmail.com
Mon Feb 10 11:57:18 EST 2003
Hello there,
need help to extract certain pieces of info from an XML file:
<?xml version="1.0"?>
<LBS>
<Geocode1>
<Restaurant>
<Name>McDonalds</Name>
<Description>One of the world's most popular
restaurants</Description>
<Name>Burger King</Name>
<Description>One of the world's most popular
restaurants</Description>
</Restaurant>
</Geocode1>
<Geocode2>
<Pubs>
<Name>Wheelwright's Arms</Name>
<Description>A quiet pub located at the heart of the town near the
market place</Description>
</Pubs>
<Shopping>
<Name>Woolworths</Name>
<Description>Woolworths - well worth it! Shop for CDs, DVDs, Games,
Toys</Description>
</Shopping>
</Geocode2>
<Geocode3>
<Shopping>
<Name>Argos</Name>
<Description>Catalogue Shopping with a wide range of
products</Description>
</Shopping>
</Geocode3>
<Geocode4>
<Banking>
<Name>Barclays</Name>
<Description>Barclays Bank</Description>
</Banking>
<Shopping>
<Name>Dixons</Name>
<Description>For all your electronic accessories, camcorders,
computing, gaming, Hi-Fi, mobile, peripherals, photograph, TV,
video and DVD needs in one place</Description>
</Shopping>
<Pubs>
<Name>Hog's Head</Name>
<Description>A friendly pub with a wide range of drinks perfectly
suited for any taste</Description>
</Pubs>
</Geocode4>
<Geocode5>
<Banking>
<Name>HSBC</Name>
<Description>HSBC - The World's Local Bank</Description>
</Banking>
<Shopping>
<Name>Superdrug</Name>
<Description>For all your toiletry needs</Description>
<Name>Marks and Spencer</Name>
<Description>For all your men, women or kids cloths, gifts, food,
furniture needs</Description>
</Shopping>
</Geocode5>
<Geocode6>
<Restaurant>
<Name>Little Italy</Name>
<Description>Italian specialty - Pizzas, pastas</Description>
<Name>Pizza Hut</Name>
<Description>Innovative Pizzas for any taste</Description>
</Restaurant>
<Entertainment>
<Name>Rotunda</Name>
<Description>An all in one entertainment center with cinemas,
restaurants, cafes, leisure clubs and a bowling
complex</Description>
<Name>Bingo</Name>
<Description>If you enjoy playing Bingo, then this is the place for
you</Description>
</Entertainment>
</Geocode6>
</LBS>
I am using DOM and the code below simply displays the above XML onto
the screen (without tags). What I want to do is search through the
tags (using getElementsByTagName or something similar) to extract
information such as bank tags only or shopping tags...(you get the
idea). I was just getting the hang of Python and I have to go and do
something so complex. Thanks in advance
from xml.dom import minidom
# this opens up the xml file to find the nearest user specified
location
xmldoc = minidom.parse('database.xml')
print "<p>"
# display entire XML document on screen
print xmldoc.toxml()
# this is where the parsing begins
More information about the Python-list
mailing list