Solved

Getting parent node attribute values from XML files

Posted on 2006-06-30
8
423 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:JohnAutoSales
  • 4
  • 3
8 Comments
 
LVL 15

Expert Comment

by:efn
ID: 17024159
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
 
LVL 28

Assisted Solution

by:pepr
pepr earned 450 total points
ID: 17024843
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
 
LVL 28

Expert Comment

by:pepr
ID: 17024849
... you need at least Python 2.4 that implements the built-in sets, or you have to use the module for sets.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 28

Expert Comment

by:pepr
ID: 17024873
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
 

Author Comment

by:JohnAutoSales
ID: 17032547
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
 
LVL 15

Accepted Solution

by:
efn earned 50 total points
ID: 17033391
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
 
LVL 28

Expert Comment

by:pepr
ID: 17035236
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
 
LVL 15

Expert Comment

by:efn
ID: 17035340
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

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python multiple IF statements 4 83
Randomize in Owl Carousel v1.3.2 6 45
wipe a usb using python 5 48
Python 3.5.2 - Script Help 3 17
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question