Solved

Finding values of many XML attributes and assigning them to variables

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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.
The viewer will learn how to dynamically set the form action using jQuery.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

762 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

21 Experts available now in Live!

Get 1:1 Help Now