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:\Document s and Settings\All Users\Documents\Garage\gar age.xml')
garage = xmldoc.firstChild
vehicleNode = xmldoc.getElementsByTagNam e("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
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:\Document
garage = xmldoc.firstChild
vehicleNode = xmldoc.getElementsByTagNam
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
Sorry for the last post.....I had the filename in wrong.
Working great now!!
THANKS!
Working great now!!
THANKS!
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.