XML pickle

castironpi at gmail.com castironpi at gmail.com
Fri Feb 15 02:53:36 CET 2008


> I cannot tell if the above approach will solve your problem or not.

Well, declare me a persistent object.

from lxml import etree

SS= '{urn:schemas-microsoft-com:office:spreadsheet}'
book= etree.Element( 'Workbook' )
book.set( 'xmlns', 'urn:schemas-microsoft-com:office:spreadsheet' )
sheet= etree.SubElement(book, "Worksheet")
sheet.set( SS+ 'Name', 'WSheet1' )
table= etree.SubElement(sheet, "Table")
row= etree.SubElement(table, "Row")
cell1= etree.SubElement(row, "Cell")
data1= etree.SubElement(cell1, "Data" )
data1.set( SS+ 'Type', "Number" )
data1.text= '123'
cell2= etree.SubElement(row, "Cell")
data2= etree.SubElement(cell2, "Data" )
data2.set( SS+ 'Type', "String" )
data2.text= 'abc'
out= etree.tostring( book, pretty_print= True, xml_declaration=True )
print( out )
open( 'xl.xml', 'w' ).write( out )

Can you use set( '{ss}Type' ) somehow?  And any way to make this look
closer to the original?  But it works.

<?xml version='1.0' encoding='ASCII'?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet">
  <Worksheet xmlns:ns0="urn:schemas-microsoft-com:office:spreadsheet"
ns0:Name="WSheet1">
    <Table>
      <Row>
        <Cell>
          <Data ns0:Type="Number">123</Data>
        </Cell>
        <Cell>
          <Data ns0:Type="String">abc</Data>
        </Cell>
      </Row>
    </Table>
  </Worksheet>
</Workbook>



More information about the Python-list mailing list