Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Putting XML attribute tags in a list

Posted on 2006-06-28
4
Medium Priority
?
274 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

604 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