简体   繁体   中英

Writing XML to file using LXML

I'm trying to create a XML file using LXML. So far I have ...

from lxml import etree

def exportAsXML(self, filename):
    fields = [
        ('Realm', self.Realm),
        ('ActiveState', self.ActiveState),
        ('Name', self.Name),
        ('Type', self.Type),
        ('Level', self.Level),
        ('Quality', self.Quality),
        ('Bonus', self.Bonus),
        ('AFDPS', self.AFDPS),
        ('Speed', self.Speed),
        ('Origin', self.Origin),
        ('DamageType', self.DamageType),
        ('LeftHand', self.LeftHand),
        ('Requirement', self.Requirement),
        ('Notes', self.Notes,)
    ]

    root = etree.Element('Item')
    for key, value in fields:
        if value != '':
            etree.SubElement(root, key).text = value

    document = open(filename, 'w')
    document.write(etree.tostring(root, pretty_print = True))
    document.close()

This results in TypeError: must be str, not bytes but I don't understand what is going on. I assume by using etree.tostring that the etree object is converted to a string, but that doesn't seem to be the case. Any idea?

** UPDATE **

What I'm trying to do is create a valid XML file that is neatly formatted using LXML and contains a DTD (not implemented yet). I already make use of LXML in other areas of my application, so I figured I would continue on with it. I'm using Python 3.

** UPDATE 2 **

console

b'<Item><Realm>All</Realm><ActiveState>Dropped</ActiveState><Name>Band</Name><Type>Wrist</Type><Level>50</Level><Quality>100</Quality><Bonus>35</Bonus><Origin>Quest</Origin></Item>'

file

<Item><Realm>All</Realm><ActiveState>Dropped</ActiveState><Name>Band</Name><Type>Wrist</Type><Level>50</Level><Quality>100</Quality><Bonus>35</Bonus><Origin>Quest</Origin></Item>

For XML files, simply use the 'wb' argument in open() and consider context manager, with :

with open(filename, 'wb') as doc:
   doc.write(etree.tostring(root, pretty_print = True))

As demonstration, below tests OP's routine with slight change to self variables with output in pretty print format:

def exportAsXML(filename):
    fields = [
        ('Realm', 'self.Realm'),
        ('ActiveState', 'self.ActiveState'),
        ('Name', 'self.Name'),
        ('Type', 'self.Type'),
        ('Level', 'self.Level'),
        ('Quality', 'self.Quality'),
        ('Bonus', 'self.Bonus'),
        ('AFDPS', 'self.AFDPS'),
        ('Speed', 'self.Speed'),
        ('Origin', 'self.Origin'),
        ('DamageType', 'self.DamageType'),
        ('LeftHand', 'self.LeftHand'),
        ('Requirement', 'self.Requirement'),
        ('Notes', 'self.Notes')
    ]

    root = etree.Element('Item')
    for key, value in fields:
        if value != '':
            etree.SubElement(root, key).text = value

    with open(filename, 'wb') as doc:
        doc.write(etree.tostring(root, pretty_print = True))

exportAsXML('Output.xml')

Output

<Item>
  <Realm>self.Realm</Realm>
  <ActiveState>self.ActiveState</ActiveState>
  <Name>self.Name</Name>
  <Type>self.Type</Type>
  <Level>self.Level</Level>
  <Quality>self.Quality</Quality>
  <Bonus>self.Bonus</Bonus>
  <AFDPS>self.AFDPS</AFDPS>
  <Speed>self.Speed</Speed>
  <Origin>self.Origin</Origin>
  <DamageType>self.DamageType</DamageType>
  <LeftHand>self.LeftHand</LeftHand>
  <Requirement>self.Requirement</Requirement>
  <Notes>self.Notes</Notes>
</Item>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM