where is the Write method of ElementTree??

G

gray.bowman

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")
 
J

John Machin

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
 
S

Stefan Behnel

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 guess you did

from xml.etree import ElementTree

Then you should do this:

ElementTree.ElementTree(sectionElement).write("section.xml")

sadly, the module names in ET are upper case and look like classes...

Stefan
 
G

gray.bowman

I guess you did

from xml.etree import ElementTree

Then you should do this:

ElementTree.ElementTree(sectionElement).write("section.xml")

sadly, the module names in ET are upper case and look like classes...

Stefan

That was it! Thanks to both of you for helping.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top