Solved

Finding values of many XML attributes and assigning them to variables

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

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…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

717 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