473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Generating header information using ElementTree

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
8 5460
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
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
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

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
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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: André | last post by:
I've started using elementtree and don't understand how to use it to manipulate and replace nodes. I know how to do this using a simple, but inefficient parser I wrote, but I'd rather learn to use...
4
by: Lee Franke | last post by:
The application that I want to use requires the header to contain a username/password. Example: <soap:Header> <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/04/secext">...
0
by: Albertas | last post by:
Hello, I have a question. But first my situation: I had a Web Service, which was written using .NET Framework 2.0, here is my code: public class Service : System.Web.Services.WebService {...
3
by: sean.j.gage | last post by:
We are building a web service for a client application that will need to send information in the Header of the web service responses. What's the best way to access this header information from the...
3
by: Jan Danielsson | last post by:
Hello all, This is probably a mind numbingly brain dead question.. But how do I generate the following: <p>Current date:<br/>2000-01-01</p> ..using ElementTree? The <pelement kind of needs...
8
by: mcserret | last post by:
I know this is a recurring problem that has been addressed here before, but even after reading all that has gone before, I am still stumped. I have a form that is designed to send data to a PHP...
2
by: pks83 | last post by:
Hello I am also facing this problem when i amtrying to send additional header information. Warning: Cannot modify header information - headers already sent by (output started at header.php:96)...
10
by: jessica87 | last post by:
hi there, i m using this coding to retrieve the file from database so that my user can download the file... <?php if (!isset($_GET)) die('Invalid Parameter'); include...
3
by: Vivek Ganti | last post by:
Hi, Scenario: I have a stored .pcap file of Wireshark. Using command prompt (tshark command), i want to convert the .pcap file into a text file and store it with all information extracted...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.