where is the Write method of ElementTree??

John Machin sjmachin at lexicon.net
Thu May 22 18:37:23 EDT 2008


On May 23, 6:56 am, gray.bow... at gmail.com wrote:
> I'm messing around with trying to write an xml file using
> xml.etree.ElementTree.  All the examples on the internet show the use
> of ElementTree.write(), although when I try to use it it's not
> available, gives me ...
>
>    ElementTree(sectionElement).write("section.xml")
> TypeError: 'module' object is not callable
>
> I'm new to python, so I think I'm doing something wrong.. any
> thoughts?
>
> Thanks.'
>
> See code below:
>
> # Create xml structure
> sectionElement = ElementTree.Element("section")
>
> # step through feed result and create elements for each article -
> result[article]
> for article in feedResult:
>     articleElement = ElementTree.Element("article")
>
>     titleElement = ElementTree.SubElement(articleElement, "title")
>     titleElement.text = article['title']
>
>     bodyElement = ElementTree.SubElement(articleElement, "body")
>     bodyElement.text = article['body']
>
>     sectionElement.append(articleElement)
>
> #print ElementTree.tostring(sectionElement)
> ElementTree(sectionElement).write("section.xml")

It's complaining about
    ElementTree(whatever)
As it says, you are trying to call a module.

Looks like you need:
    sectionElement.write("section.xml")

HTH,
John



More information about the Python-list mailing list