?
Solved

Finding values of many XML attributes and assigning them to variables

Posted on 2006-06-22
3
Medium Priority
?
479 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
[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
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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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 count occurrences of each item in an array.
Suggested Courses

770 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