Link to home
Start Free TrialLog in
Avatar of JohnAutoSales
JohnAutoSales

asked on

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
Avatar of efn
efn

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.
SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
... you need at least Python 2.4 that implements the built-in sets, or you have to use the module for sets.
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

Avatar of JohnAutoSales

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.