Link to home
Start Free TrialLog in
Avatar of JohnAutoSales
JohnAutoSales

asked on

Putting XML attribute tags in a list

Hi there,

I've got an XML file of the following structure:

<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>

I need a little python script that loops through the garage node, finds all vehicle nodes and stores their makes and models in unique two member lists, i.e.
vehicle1 = [make,model]
vehicle2 = [make,model]


So far, I've got:
"
from xml.dom import minidom
xmldoc = minidom.parse('C:\Documents and Settings\All Users\Documents\Garage\garage.xml')
garage = xmldoc.firstChild

vehicleNode = xmldoc.getElementsByTagName("vehicle")
vehicleNode

for vehicleNode in garage:
"

I'm not sure how I do the loop that creates the two membered lists that have a unique name (for each vehicle).


Anyone?

Thanks,
John

   

Avatar of ramrom
ramrom
Flag of United States of America image

getElementsByTagName returns a list of vehicleNodes. Each node has an attribute named attributes, which is a dictionary with keys "make" and "model".

If you have no need to know which garage they are in, that is sufficient.

It is never a good idea in Python to create variables with situation-dependent names. This is what dictionaries or lists are for.

Is that enough to get you started? Since we don't know where you are going with this (unless it relates to your other question) its hard to give more advice.
ASKER CERTIFIED 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
Avatar of JohnAutoSales
JohnAutoSales

ASKER

Okay thanks,

I tried your code pepr, but only get the following empty list as output:

[]


I'm very new to Python, but have done other basic programming.  I can follow your code, but was surprised to see that you didn't need to declare vehicle or car in the for loops.

Any ideas why I am just getting []?


Thanks,
John

Sorry for the last post.....I had the filename in wrong.


Working great now!!
THANKS!