xml.dom.minidom

Paul Boddie paul at boddie.net
Mon Jan 20 06:42:00 EST 2003


"Graeme" <gsmatthew at ozemail.com.au> wrote in message news:<XZuW9.443$f8.10917 at nnrp1.ozemail.com.au>...
> Hi all, im still  struggling to try and find some examples on the web, im
> finding the documentation conclusive butcant find any everday samples.

I've written this introduction which you might find useful:

  http://www.boddie.org.uk/python/XML_intro.html

> I am trying to extract the values for each of a list of orderlines for a
> given order, using MSXML 3.0 (using XPath) I have no problem, but I have
> only decided in the last two weeks to write a commercial app in Python after
> discounting ruby (due to lack of documentation) so im still a bit aloof :)

I noticed in your other posting that you used case-insensitive element
names when searching for elements. Do not do this! I suppose this is a
feature that MSXML has enabled by default, but you will be frustrated
very easily if you attempt to rely on it elsewhere.

> e.g.
> 
> <order>
> <orderid>12345</orderid>
> <orderline>
> <partno>1234</partno>
> </orderline>
> <orderline>
> <partno>1235</partno>
> </orderline>
> <order>

Fixing the closing tag to "</order>", putting the above into a string,
and using the following code, I get a result list of two elements.

  # Use 'parse' with file objects.
  # Here, I've just put your document into the string 's'.
  d = xml.dom.minidom.parseString(s)
  d.getElementsByTagName("orderline")

> my current attempt is using orderNode.getElementsByTagName('orderline')

If 'orderNode' refers to an "<order>...</order>" section of the
document, then you should only expect one element in your result -
'getElementsByTagName' only searches below the current node.

> Im trying to retireve a list of Nodes i.e NodeList as per doco then extract
> each element value

Here's what you get:

  [<DOM Element: orderline at 9904468>, <DOM Element: orderline at
9904932>]

Using the usual Python list operations, you can get at each element,
but you may need to think more clearly about what "element value" you
want to get. Perhaps you might want to retrieve the part numbers.

A recommendation: if you install 4Suite, you get XPath support in
PyXML.

Paul




More information about the Python-list mailing list