?
Solved

Finding values of many XML attributes and assigning them to variables

Posted on 2006-06-22
3
Medium Priority
?
486 Views
Last Modified: 2013-11-19
Hi,

I am new to Python, and need help doing the following:

I have data stored in an XML file like this:  I can find the value of a particular attribute in a particular tag, by the following

<garage type="sales">
  <vehicle make="toyota" model="yaris">
    <note>
      New model - 1 litre Terra
    </note>
    <features>
      Electric windows, passenger airbag, ABS, immobiliser
    </features>
    <extras>
      Sunroof, air-conditioning, metallic paint
    </reference>
  </vehicle>
  <vehicle>
     Another vehicle
  </vehicle>
  <vehicle>
     Another vehicle
  </vehicle>
<garage>

I can find the value of a particular attribute in a particular tag (in this case the make of a vehicle), by the following:

from xml.dom import minidom
xmldoc = minidom.parse('C:\Documents and Settings\All Users\Documents\Programming\GarageInfo.xml')
vehicletag = xmldoc.getElementsByTagName('vehicle')
vehicletag
vehicletaglist = vehicletag[0]

vehicletaglist.attributes["make"]
vehiclename = vehicletaglist.attributes["make"]
vehiclename
vehiclename.value


What I need to be able to do is find the values of 'make' from ALL the 'vehicle' tags, and then to store these values in variables or somewhere, so that I can access then later in the programme.

Some kind of loop probably?


Hope someone can help,

John
0
Comment
Question by:JohnAutoSales
  • 2
3 Comments
 
LVL 7

Expert Comment

by:Chatable
ID: 16963556
Yes, you can iterate throught the XML using firstChild (to go into deeper tags) and nextSibling (to go through tags at the same level). There are also lastChild, previousSibling, etc...
First there are some errors in your XML file. You should change the </reference> to </extras> and the <garage> at the end of the XML document to </garage> to make the XML valid.
I've corrected the XML and also added make attributes to the other vehicles:

<garage type="sales">
  <vehicle make="toyota" model="yaris">
    <note>
      New model - 1 litre Terra
    </note>
    <features>
      Electric windows, passenger airbag, ABS, immobiliser
    </features>
    <extras>
      Sunroof, air-conditioning, metallic paint
    </extras>
  </vehicle>
  <vehicle make="ford">
     Another vehicle
  </vehicle>
  <vehicle make="mazda">
     Another vehicle
  </vehicle>
</garage>

Here's a code that collects all the "make" attributes to a list:
from xml.dom import minidom
xmldoc = minidom.parse('C:\Documents and Settings\All Users\Documents\Programming\GarageInfo.xml')
vehicles = []
garage = xmldoc.firstChild
while garage.nodeName != 'garage':
      garage = garage.nextSibling
current_vehicle = garage.firstChild
while current_vehicle != None:
      if current_vehicle.nodeName == 'vehicle':
            vehicles.append(current_vehicle.attributes['make'].value)
      current_vehicle = current_vehicle.nextSibling
0
 

Author Comment

by:JohnAutoSales
ID: 16964609
Okay,

I think that's working okay.  But how do I access the values that I've stored in the list.

For example, I would like to output the values in an HTML file, with each values in a <p> tag.


For the above example, it should give output like:

<p>toyota</p>
<p>ford</p>
<p>mazda</p>


Figure out this last little bit, and you've really deserved the points!

Thanks,
John
0
 
LVL 7

Accepted Solution

by:
Chatable earned 2000 total points
ID: 16967391
I'm not sure that the XML module is the best module for HTML. XML is a one type of the SGML format HTML is another type of SGML, but HTML is not a type of XML. For instance, HTML is much more tolearable towards mistakes than XML, and also not all HTML tags are paired (see the <BR> tag for instance). That's why the feasibility of an HTML document being successfully parsed by the XML parser is extremely low.
However, to get the text inside a paired tag you only need to go to the firstChild in the tag. Text appears in the Python XML module just like the tags, except that it's nodeName is always #text
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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

862 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