473,748 Members | 8,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5490
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.xm l", '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.xm l", '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.xm l", '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.xm l", '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(Reco rdName, key)
test.text = str(record[key])
tree = ElementTree(roo t)
tree.write(XMLF ileBaseName)

def main():
outFile = open("record.xm l", '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(Reco rdName, key)
test.text = str(record[key])
tree = ElementTree(roo t)
tree.write(XMLF ileBaseName)
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.xm l", '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(Reco rdName, key)
test.text = str(record[key])
tree = ElementTree(roo t)
tree.write(XMLF ileBaseName)

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.xm l", '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(Reco rdName, key)
test.text = str(record[key])
tree = ElementTree(roo t)
tree.write(XMLF ileBaseName)
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.xm l", '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
7088
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 a better tool - especially as it is to be added to the standard library. I'll use a simple, but representative example. Suppose I have the following text, where I inserted blank lines to isolate the part I want to manipulate....
4
8605
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"> <wsse:UsernameToken> <wsse:Username>fluffy</wsse:Username> <wsse:Password Type="wsse:PasswordText">bunny</wsse:Password>
0
5065
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 { public class Authentication : SoapHeader
3
2337
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 client application which is written in C#?
3
2060
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 two text blocks, as far as I can tell?
8
2397
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 page where is is to be validated then read into a MySQL table. Once that is done, the form is to send the user either back to the originating page, or to a logout page. I have several versions of this setup on my site that work well, so
2
1487
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) in my.class.php on line 1198 What is happening over here is that i when i am trying to download an attached file i am passing the additional header information which is conflicting with the header info which has already being sent before. ...
10
2380
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 'connectdb.php'; $id = $_GET;
3
13027
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 from the .pcap file. How should i do this? Currently, when i do this:
0
9534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9241
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.