By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,091 Members | 1,555 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,091 IT Pros & Developers. It's quick & easy.

Generating header information using ElementTree

P: n/a
Hi there,

I'm only new to Python so please bear with me. I using ElementTree to
generate an XML file that will reference a DTD and an XSL file. The
header information I want at the start of the file is as follows:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<!DOCTYPE BobActivityLog SYSTEM "test.dtd">

How do you add this header information to the tree as I can't find any
documentation or examples on how you can do this. Any help would be
appreciated. Thank you and good luck.
Craig Williamson

Nov 26 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Craig wrote:
I'm only new to Python so please bear with me. I using ElementTree to
generate an XML file that will reference a DTD and an XSL file. The
header information I want at the start of the file is as follows:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<!DOCTYPE BobActivityLog SYSTEM "test.dtd">

How do you add this header information to the tree
to the file, you mean? use print.

</F>

Nov 26 '06 #2

P: n/a
Fredrik Lundh wrote:
Craig wrote:
I'm only new to Python so please bear with me. I using ElementTree to
generate an XML file that will reference a DTD and an XSL file. The
header information I want at the start of the file is as follows:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<!DOCTYPE BobActivityLog SYSTEM "test.dtd">

How do you add this header information to the tree

to the file, you mean? use print.

</F>
Thanks for the quick response. Here is the code I am using to manually
write to the file is:

outFile = open("record.xml", 'w')
outFile.write = ("<?xml version=\"1.0\"?>\n")
outFile.write = ("<?xml-stylesheet type=\"text/xsl\"
href=\"test.xsl\"?>\n")
outFile.write = ("<!DOCTYPE BobActivityLog SYSTEM \"test.dtd\">\n")
outFile.close()

When I run the code I get the following error message:

Traceback (most recent call last):
File "bob-xml.py", line 189, in <module>
main()
File "bob-xml.py", line 173, in main
outFile.write = ("<?xml version=\"1.0\"?>\n")
AttributeError: 'file' object attribute 'write' is read-only

What am I doing wrong here? If anyone could help that would be
appreciated. Thanks again.
Craig

Nov 26 '06 #3

P: n/a
Craig schrieb:
Fredrik Lundh wrote:
>Craig wrote:
>>I'm only new to Python so please bear with me. I using ElementTree to
generate an XML file that will reference a DTD and an XSL file. The
header information I want at the start of the file is as follows:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<!DOCTYPE BobActivityLog SYSTEM "test.dtd">

How do you add this header information to the tree
to the file, you mean? use print.

</F>

Thanks for the quick response. Here is the code I am using to manually
write to the file is:

outFile = open("record.xml", 'w')
outFile.write = ("<?xml version=\"1.0\"?>\n")
outFile.write = ("<?xml-stylesheet type=\"text/xsl\"
href=\"test.xsl\"?>\n")
outFile.write = ("<!DOCTYPE BobActivityLog SYSTEM \"test.dtd\">\n")
outFile.close()

When I run the code I get the following error message:

Traceback (most recent call last):
File "bob-xml.py", line 189, in <module>
main()
File "bob-xml.py", line 173, in main
outFile.write = ("<?xml version=\"1.0\"?>\n")
AttributeError: 'file' object attribute 'write' is read-only

What am I doing wrong here? If anyone could help that would be
appreciated. Thanks again.
write is a method, not a property.

outFile.write("whatever")

is the way to go...

Diez
Nov 26 '06 #4

P: n/a

Craig wrote:
Fredrik Lundh wrote:
Craig wrote:
I'm only new to Python so please bear with me. I using ElementTree to
generate an XML file that will reference a DTD and an XSL file. The
header information I want at the start of the file is as follows:
>
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<!DOCTYPE BobActivityLog SYSTEM "test.dtd">
>
How do you add this header information to the tree
to the file, you mean? use print.

</F>

Thanks for the quick response. Here is the code I am using to manually
write to the file is:

outFile = open("record.xml", 'w')
outFile.write = ("<?xml version=\"1.0\"?>\n")
outFile.write is a *method* that you call to write your string, not
some sort of magic gadget that writes stuff as a side-effect of being
assigned to. Simply do this:

outFile.write("<?xml version=\"1.0\"?>\n")

and (of course) fix the following similar problems.

outFile.write = ("<?xml-stylesheet type=\"text/xsl\"
href=\"test.xsl\"?>\n")
outFile.write = ("<!DOCTYPE BobActivityLog SYSTEM \"test.dtd\">\n")
outFile.close()

When I run the code I get the following error message:

Traceback (most recent call last):
File "bob-xml.py", line 189, in <module>
main()
File "bob-xml.py", line 173, in main
outFile.write = ("<?xml version=\"1.0\"?>\n")
AttributeError: 'file' object attribute 'write' is read-only
If that were permitted, you'd be replacing the method with a string!

HTH,
John

Nov 26 '06 #5

P: n/a
Diez B. Roggisch wrote:
Craig schrieb:
Fredrik Lundh wrote:
Craig wrote:

I'm only new to Python so please bear with me. I using ElementTree to
generate an XML file that will reference a DTD and an XSL file. The
header information I want at the start of the file is as follows:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<!DOCTYPE BobActivityLog SYSTEM "test.dtd">

How do you add this header information to the tree
to the file, you mean? use print.

</F>
Thanks for the quick response. Here is the code I am using to manually
write to the file is:

outFile = open("record.xml", 'w')
outFile.write = ("<?xml version=\"1.0\"?>\n")
outFile.write = ("<?xml-stylesheet type=\"text/xsl\"
href=\"test.xsl\"?>\n")
outFile.write = ("<!DOCTYPE BobActivityLog SYSTEM \"test.dtd\">\n")
outFile.close()

When I run the code I get the following error message:

Traceback (most recent call last):
File "bob-xml.py", line 189, in <module>
main()
File "bob-xml.py", line 173, in main
outFile.write = ("<?xml version=\"1.0\"?>\n")
AttributeError: 'file' object attribute 'write' is read-only

What am I doing wrong here? If anyone could help that would be
appreciated. Thanks again.

write is a method, not a property.

outFile.write("whatever")

is the way to go...

Diez
Great. Got that sorted. The problem I have now is that some of the
XML data is not copied across to the file when I have the text
information included. The number of characters that is lost is equal
to the number of characters that is in the block of text I entered
before. The code I am using is:

def WriteXMLRecord ( record, XMLFileBaseName, root):
RecordName = SubElement(root, "Log")
#iterate through all the fields in the record
for key in record:
# write the key and its data
test = SubElement(RecordName, key)
test.text = str(record[key])
tree = ElementTree(root)
tree.write(XMLFileBaseName)

def main():
outFile = open("record.xml", 'w')
outFile.write("""<?xml version=\"1.0\"?>
<?xml-stylesheet type=\"text/xsl\" href=\"test.xsl\"?>
<!DOCTYPE BobActivityLog SYSTEM \"test.dtd\">\n\n""")

root = Element("Log")
WriteXMLRecord(data1, "record.xml", root)
WriteXMLRecord(data2, "record.xml", root)
WriteXMLRecord(data3, "record.xml", root)
outFile.close()

Any help would be appreciated. Thanks and good luck.
Craig

Nov 26 '06 #6

P: n/a

Craig wrote:
Great. Got that sorted. The problem I have now is that some of the
XML data is not copied across to the file when I have the text
information included. The number of characters that is lost is equal
to the number of characters that is in the block of text I entered
before. The code I am using is:

def WriteXMLRecord ( record, XMLFileBaseName, root):
RecordName = SubElement(root, "Log")
#iterate through all the fields in the record
for key in record:
# write the key and its data
test = SubElement(RecordName, key)
test.text = str(record[key])
tree = ElementTree(root)
tree.write(XMLFileBaseName)
I'm guessing, based on reading the docs for the write method, that you
should be using the file handle, rather than the file name, if the file
is already opened. So (1) change the name of the 2nd arg to
XMLFileHandle or somesuch, and in the caller, use outFile (the handle)
instead of "record.xml".
>
def main():
outFile = open("record.xml", 'w')
outFile.write("""<?xml version=\"1.0\"?>
<?xml-stylesheet type=\"text/xsl\" href=\"test.xsl\"?>
<!DOCTYPE BobActivityLog SYSTEM \"test.dtd\">\n\n""")

root = Element("Log")
WriteXMLRecord(data1, "record.xml", root)
WriteXMLRecord(data2, "record.xml", root)
WriteXMLRecord(data3, "record.xml", root)
outFile.close()
HTH,
John

Nov 26 '06 #7

P: n/a

John Machin wrote:
Craig wrote:
Great. Got that sorted. The problem I have now is that some of the
XML data is not copied across to the file when I have the text
information included. The number of characters that is lost is equal
to the number of characters that is in the block of text I entered
before. The code I am using is:

def WriteXMLRecord ( record, XMLFileBaseName, root):
RecordName = SubElement(root, "Log")
#iterate through all the fields in the record
for key in record:
# write the key and its data
test = SubElement(RecordName, key)
test.text = str(record[key])
tree = ElementTree(root)
tree.write(XMLFileBaseName)

I'm guessing, based on reading the docs for the write method, that you
should be using the file handle, rather than the file name, if the file
is already opened. So (1) change the name of the 2nd arg to
XMLFileHandle or somesuch, and in the caller, use outFile (the handle)
instead of "record.xml".

def main():
outFile = open("record.xml", 'w')
outFile.write("""<?xml version=\"1.0\"?>
<?xml-stylesheet type=\"text/xsl\" href=\"test.xsl\"?>
<!DOCTYPE BobActivityLog SYSTEM \"test.dtd\">\n\n""")

root = Element("Log")
WriteXMLRecord(data1, "record.xml", root)
WriteXMLRecord(data2, "record.xml", root)
WriteXMLRecord(data3, "record.xml", root)
outFile.close()
HTH,
John
Great. Got it. Thanks so much for all your help.
Craig

Nov 27 '06 #8

P: n/a
And dont forget that you can use triple quotes to get rid of all the
quote-escaping you are using:

$ python
>>"""
.... <elem attr="hi">
.... ho
.... </elem>
.... """
'\n<elem attr="hi">\nho\n</elem>\n'
>>>

Craig wrote:
John Machin wrote:
Craig wrote:
Great. Got that sorted. The problem I have now is that some of the
XML data is not copied across to the file when I have the text
information included. The number of characters that is lost is equal
to the number of characters that is in the block of text I entered
before. The code I am using is:
>
def WriteXMLRecord ( record, XMLFileBaseName, root):
RecordName = SubElement(root, "Log")
#iterate through all the fields in the record
for key in record:
# write the key and its data
test = SubElement(RecordName, key)
test.text = str(record[key])
tree = ElementTree(root)
tree.write(XMLFileBaseName)
I'm guessing, based on reading the docs for the write method, that you
should be using the file handle, rather than the file name, if the file
is already opened. So (1) change the name of the 2nd arg to
XMLFileHandle or somesuch, and in the caller, use outFile (the handle)
instead of "record.xml".
>
def main():
outFile = open("record.xml", 'w')
outFile.write("""<?xml version=\"1.0\"?>
<?xml-stylesheet type=\"text/xsl\" href=\"test.xsl\"?>
<!DOCTYPE BobActivityLog SYSTEM \"test.dtd\">\n\n""")
>
root = Element("Log")
WriteXMLRecord(data1, "record.xml", root)
WriteXMLRecord(data2, "record.xml", root)
WriteXMLRecord(data3, "record.xml", root)
outFile.close()
>
HTH,
John

Great. Got it. Thanks so much for all your help.
Craig
Nov 27 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.