Solved

Putting XML attribute tags in a list

Posted on 2006-06-28
4
269 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
  • 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 28

Accepted Solution

by:
pepr earned 500 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

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