Element tree errors

S

saif.shakeel

HI,
The following code is for replacing the strings localid with
"datapackageid" in an xml document.:

from xml.etree.ElementTree import ElementTree as et

file_input = raw_input("Enter The ODX File Path:")
(shortname,ext)=os.path.splitext(file_input)
test_file=shortname+"testxml.xml"

input_xml = open(file_input,'r')

tree = et.parse(input_xml)

for t in tree.getiterator("SERVICEPARAMETER"):
if t.get("Semantics") == "localId":
t.set("Semantics", "dataPackageID")

tree.write(test_file)

The python ver is 2.5.there is no module error
now,but when i run i get this error:

Traceback (most recent call last):
File "C:\Documents and Settings\pzchfr\Desktop\test.py", line 12, in
<module>
tree = et.parse(input_xml)
TypeError: unbound method parse() must be called with ElementTree
instance as first argument (got file instance instead)

I am passing the input file from user to be parsed
which seems to be incorrect,Can someone help me.
Thanks
 
J

John Machin

HI,
The following code is for replacing the strings localid with
"datapackageid" in an xml document.:

from xml.etree.ElementTree import ElementTree as et

Note there are *two* occurrences of ElementTree in what you have
above. The first is the *module* with the parse function that that you
are looking for. The second is a *class*.

Try this:

import xml.etree.ElementTree as et
input_xml = open(file_input,'r')
tree = et.parse(input_xml)

or this:

from xml.etree.ElementTree import parse
input_xml = open(file_input,'r')
tree = parse(input_xml)
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top