?
Solved

Putting XML attribute tags in a list

Posted on 2006-06-28
4
Medium Priority
?
273 Views
Last Modified: 2013-11-19
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

   

0
Comment
Question by:JohnAutoSales
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 17

Expert Comment

by:ramrom
ID: 17006586
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.
0
 
LVL 29

Accepted Solution

by:
pepr earned 2000 total points
ID: 17007276
Try the example, store it in vehicles.py
-------------------------------------------------
from xml.dom import minidom
xmldoc = minidom.parse('garage.xml')
garage = xmldoc.firstChild

vehicleLst = xmldoc.getElementsByTagName("vehicle")

myCarPark = []
for vehicle in vehicleLst:
    carAttr = []
    carAttr.append(vehicle.getAttribute('make'))
    carAttr.append(vehicle.getAttribute('model'))

    myCarPark.append(carAttr)

print myCarPark

i = 1
for car in myCarPark:
    print '%2d.' % i, car[0].capitalize(), car[1].capitalize()
    i = i + 1
-------------------------------------------------

I did try it on my local file garage.xml
-------------------------------------------------
<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>
  <vehicle make="skoda" model="octavia">
    <note>
      New model
    </note>
  </vehicle>
  <vehicle make="skoda" model="fabia">
    <note>
      Almost new
    </note>
  </vehicle>
  <vehicle make="trabant" model="combi">
    <note>
      Fifth hand, 15000000 miles
    </note>
  </vehicle>
</garage>
-------------------------------------------------

The result looks like:
-------------------------------------------------
C:\tmp\aa>python vehicles.py
[[u'toyota', u'yaris'], [u'nissan', u'note'], [u'skoda', u'octavia'], [u'skoda', u'fabia'], [u'trabant', u'combi']]
 1. Toyota Yaris
 2. Nissan Note
 3. Skoda Octavia
 4. Skoda Fabia
 5. Trabant Combi
-------------------------------------------------
0
 

Author Comment

by:JohnAutoSales
ID: 17012622
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

0
 

Author Comment

by:JohnAutoSales
ID: 17012911
Sorry for the last post.....I had the filename in wrong.


Working great now!!
THANKS!
0

Featured Post

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

777 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