Solved

Finding values of many XML attributes and assigning them to variables

Posted on 2006-06-22
3
470 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 500 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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 …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

773 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