Solved

Putting XML attribute tags in a list

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
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…
The viewer will learn how to dynamically set the form action using jQuery.

696 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