Getting parent node attribute values from XML files

Hi,

I have an XML file:

<garage name="wood road">
  <vehicle make="toyota" model="yaris">
    <note>
      New model - 1 litre Terra
    </note>
   </vehicle>
   <vehicle make="nissan" model="note">
    <note>
      Second hand - red, 15000 miles
    </note>
   </vehicle>
   ...
</garage>
<garage name="john street">
  <vehicle make="ford" model="mondeo">
    <note>
      New car - black, air-con fitted
    </note>
   </vehicle>
   <vehicle make="mazda" model="323">
    <note>
      Second hand - silver, 35000 miles
    </note>
   </vehicle>
   ...
</garage>



I need to first of all find the makes of all the vehicles.  This I can do using:


vehicleLst = xmldoc.getElementsByTagName("vehicle")

vehicleMake = []
for vehicle in vehicleLst:
    vehicleMake.append(modification.getAttribute('make'))


For each of the makes in this list, I then need to find the name of the garage where these vehicles are housed.


I've been trying to use parentNode for this, but to no avail.
If I could have the vehicle makes and the corresponding garages together in a list (within a list) or a dictionary, it would be great.



John
JohnAutoSalesAsked:
Who is Participating?
 
efnCommented:
set() constructs an empty set object.  A set is an unordered collection of immutable values.  A module that supports sets was introduced in Python 2.3, and sets have been built-in since Python 2.4.

Set Types reference
http://docs.python.org/lib/types-set.html

Highlights:  Python 2.4
http://www.python.org/download/releases/2.4/highlights/

pepr's design put all the garages for a make in a set, whereas I had used a list.  The difference is that a set will deduplicate, so if a garage had more than one car of the same make, the list for the make would contain multiple occurrences of the same garage name, but the set would contain only one.
0
 
efnCommented:
It sounds like what you may need is a dictionary where the keys are makes and the values are lists of garages.  If you turned the XML into corresponding Python data structures, you could do something like this:

makeGarage = {}
for garage in garages:
    for vehicle in garage.vehicles:
        if makeGarage.has_key(vehicle.make):
            makeGarage[vehicle.make].append(garage.name)
        else:
            makeGarage[vehicle.make] = [garage.name]

To make this work, you would need to construct a sequence called "garages" where each element has a name string and a vehicles list, where each element of the vehicles list has a name string.  This a straightforward reflection of the XML structure you showed.  If you can't figure out how to get that out of the XML, probably someone here can advise you.
0
 
peprCommented:
If the problem become more complex, it is better to rethink the design. XML is good for storing the data, but it is not that good for using them more dynamically. You may choose the classic relational database approach, or you can simulate your reality by Python objects. Say, vehicles are good candidates for objects. One of the attributes of the vehicle may be the garage (the home garage). A garage is also a good candidate for the object -- the collection of vehicles. The following intput XML were used for testing the code:

garages.xml file
-----------------------------------------------------------
<top>
<garage name="wood road">
  <vehicle make="toyota" model="yaris">
    <note>
      New model - 1 litre Terra
    </note>
   </vehicle>
   <vehicle make="nissan" model="note">
    <note>
      Second hand - red, 15000 miles
    </note>
   </vehicle>
   ...
</garage>
<garage name="john street">
  <vehicle make="ford" model="mondeo">
    <note>
      New car - black, air-con fitted
    </note>
   </vehicle>
   <vehicle make="mazda" model="323">
    <note>
      Second hand - silver, 35000 miles
    </note>
   </vehicle>
   ...
</garage>
</top>
-----------------------------------------------------------

The following script shows the object oriented representation of vehicles and garages.

vehicles.py
-----------------------------------------------------------
import xml.dom.minidom

class Garage(object):
    """Class representing one garage with zero or more vehicles."""

    def __init__(self, name):
        self.name = name
        self.vehicles = set()

class Vehicle(object):
    """Class representing one vehicle with one 'home' garage."""  

    def __init__(self, make, model, garage):
        self.make = make
        self.model = model
        self.garage = garage

    def maker_model(self):
        """Returns only the maker and the model capitalized."""
        return '%s %s' % (self.make.capitalize(), self.model.capitalize())
       
    def __str__(self):
        """Converts the status of the object into a readable string."""
        return '%s %s, %s' % (self.make, self.model, self.garage.name)


garages = []     # list of all my garages
vehicles = []    # list of all my cars

# Extract the information from the xml file, build the objects,
# and fill the lists of objects.
xmldoc = xml.dom.minidom.parse('garages.xml')

for gelem in xmldoc.getElementsByTagName('garage'):
   
    # Create the empty garage object with the name
    # and add it to the list of all garages.
    garage = Garage(gelem.getAttribute('name'))
    garages.append(garage)
   
    # For each garage...
    for velem in gelem.getElementsByTagName('vehicle'):

        # Create the object representing the vehicle and bind it
        # to this garage (last argument -- the reference from
        # vehicle to garage).
        vehicle = Vehicle(velem.getAttribute('make'),
                          velem.getAttribute('model'),
                          garage)
       
        # Add the vehicle into garage (the reference from garage to vehicle).
        garage.vehicles.add(vehicle)
   
        # Add the vehicle to the list of all vehicles.
        vehicles.append(vehicle)



# The example of usage of the above lists:
print 'Vehicles grouped by garages.'
for garage in garages:
    print 'Garage:', garage.name
    for vehicle in garage.vehicles:
        print '    ', vehicle.maker_model()
    print


print '-' * 70
print 'All vehicles.'
for vehicle in vehicles:
    print vehicle

print '-' * 70
print 'Building the dictionary: maker -> set of garages.'
make2garages = {}
for vehicle in vehicles:
    if vehicle.make not in make2garages:
        make2garages[vehicle.make] = set()
    make2garages[vehicle.make].add(vehicle.garage.name)

print 'Garage with Toyotas.'
for garageName in make2garages['toyota']:
    print garageName
-----------------------------------------------------------

and the result looks like this...
=================================
C:\tmp\a>python vehicles.py
Vehicles grouped by garages.
Garage: wood road
     Toyota Yaris
     Nissan Note

Garage: john street
     Ford Mondeo
     Mazda 323

----------------------------------------------------------------------
All vehicles.
toyota yaris, wood road
nissan note, wood road
ford mondeo, john street
mazda 323, john street
----------------------------------------------------------------------
Building the dictionary: maker -> set of garages.
Garage with Toyotas.
wood road
=================================
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
peprCommented:
... you need at least Python 2.4 that implements the built-in sets, or you have to use the module for sets.
0
 
peprCommented:
Improvement. In the last part of the example script, it is even better to build the dictionary of maker to set of garages (i.e. not set of garage names). This way, one can use the garage object to get the vehicles inside and display only those that belong to the maker. Replace the last part of the example by:

------------------------------------------------------------------------------
print '-' * 70
print 'Building the dictionary: maker -> set of garages.'
make2garages = {}
for vehicle in vehicles:
    if vehicle.make not in make2garages:
        make2garages[vehicle.make] = set()
    make2garages[vehicle.make].add(vehicle.garage)

print 'Garage with Toyotas.'
make = 'toyota'
for garage in make2garages[make]:
    print 'Garage:', garage.name
    for vehicle in garage.vehicles:
        if vehicle.make == make:
            print '    ', vehicle.maker_model()
------------------------------------------------------------------------------
and you will get something like

----------------------------------------------------------------------
Building the dictionary: maker -> set of garages.
Garage with Toyotas.
Garage: wood road
     Toyota Yaris

0
 
JohnAutoSalesAuthor Commented:
This is excellent pepr!

Working great.  Just out of curiosity what does set() in:

class Garage(object):
    """Class representing one garage with zero or more vehicles."""

    def __init__(self, name):
        self.name = name
        self.vehicles = set()

do?


Thanks so much,
John
0
 
peprCommented:
What efn said about the set() is true. But his last sentences are slightly confused. The question is about the set in the garage. It will contain all cars even if the name are the same. You can try... and I will explain below:

garages.xml
-----------------------------------------------------------------
<top>
<garage name="wood road">
  <vehicle make="toyota" model="yaris">
    <note>
      New model - 1 litre Terra A
    </note>
  </vehicle>
  <vehicle make="toyota" model="yaris">
    <note>
      New model - 1 litre Terra B
    </note>
  </vehicle>
  <vehicle make="toyota" model="yaris">
    <note>
      New model - 1 litre Terra C
    </note>
  </vehicle>
  <vehicle make="toyota" model="yaris">
    <note>
      New model - 1 litre Terra D
    </note>
  </vehicle>
  <vehicle make="toyota" model="yaris">
    <note>
      New model - 1 litre Terra E
    </note>
  </vehicle>
  <vehicle make="nissan" model="note">
    <note>
      Second hand - red, 15000 miles
    </note>
  </vehicle>
   ...
</garage>
<garage name="john street">
  <vehicle make="ford" model="mondeo">
    <note>
      New car - black, air-con fitted
    </note>
   </vehicle>
   <vehicle make="mazda" model="323">
    <note>
      Second hand - silver, 35000 miles
    </note>
   </vehicle>
   ...
</garage>
</top>
-----------------------------------------------------------------

The result looks like
======================================
Vehicles grouped by garages.
Garage: wood road
     Toyota Yaris
     Toyota Yaris
     Nissan Note
     Toyota Yaris
     Toyota Yaris
     Toyota Yaris

Garage: john street
     Ford Mondeo
     Mazda 323

----------------------------------------------------------------------
All vehicles.
toyota yaris, wood road
toyota yaris, wood road
toyota yaris, wood road
toyota yaris, wood road
toyota yaris, wood road
nissan note, wood road
ford mondeo, john street
mazda 323, john street
----------------------------------------------------------------------
Building the dictionary: maker -> set of garages.
Garage with Toyotas.
wood road
======================================

You can see more Toyota Yaris vehicles in the wood road garage, so no one was removed. The reason is that the vehicles are represented by objects and the set contains the references to the objects, not only the same names of the vehicles. All the references are different (because all the objects are different), so no duplicities, no removal.

I have chosen sets from another reason. The set says nothing about the order of cars in the garage which better reflects the reality -- there is no inherited relation among the cars. The operation of removing the vehicle from the garage will be cheaper.

Each vehicle is referenced twice, once from the vehicles list, once from the set in the garage object. If you know the reference, then testing if the vehicle in the garage is much cheaper operation with set than the same testing with list.
0
 
efnCommented:
I was comparing values in the makeGarage dictionary in my sketch with the values in the parallel make2garages dictionary in pepr's first implementation, so I was comparing lists of strings to sets of strings, and in that context, I believe my comment was correct.  Sorry it was not clearer.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.