Solved

Putting XML attribute tags in a list

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
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:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now