Generating header information using ElementTree

Discussion in 'Python' started by Craig, Nov 26, 2006.

  1. Craig

    Craig Guest

    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
     
    Craig, Nov 26, 2006
    #1
    1. Advertising

  2. 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>
     
    Fredrik Lundh, Nov 26, 2006
    #2
    1. Advertising

  3. Craig

    Craig Guest

    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
     
    Craig, Nov 26, 2006
    #3
  4. 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
     
    Diez B. Roggisch, Nov 26, 2006
    #4
  5. Craig

    John Machin Guest

    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
     
    John Machin, Nov 26, 2006
    #5
  6. Craig

    Craig Guest

    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
     
    Craig, Nov 26, 2006
    #6
  7. Craig

    John Machin Guest

    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
     
    John Machin, Nov 27, 2006
    #7
  8. Craig

    Craig Guest

    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
     
    Craig, Nov 27, 2006
    #8
  9. Craig

    metaperl Guest

    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
     
    metaperl, Nov 27, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bit byte
    Replies:
    1
    Views:
    669
    benben
    Apr 19, 2006
  2. Tim Arnold

    extra xml header with ElementTree?

    Tim Arnold, May 25, 2007, in forum: Python
    Replies:
    6
    Views:
    411
    Steve Holden
    May 25, 2007
  3. Peter Bengtsson

    ElementTree and namespaces in the header only

    Peter Bengtsson, Jan 15, 2008, in forum: Python
    Replies:
    3
    Views:
    817
    Peter Bengtsson
    Jan 17, 2008
  4. mlt
    Replies:
    2
    Views:
    881
    Jean-Marc Bourguet
    Jan 31, 2009
  5. Kee Nethery
    Replies:
    12
    Views:
    2,128
    Stefan Behnel
    Jun 27, 2009
Loading...

Share This Page