使用LXML将XML写入文件

时间:2018-02-12 20:58:39

标签: python xml python-3.x lxml

我正在尝试使用LXML创建XML文件。到目前为止,我有......

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

这导致TypeError: must be str, not bytes,但我不明白发生了什么。我假设使用etree.tostring将etree对象转换为字符串,但似乎并非如此。有什么想法吗?

**更新**

我尝试做的是创建一个有效的XML文件,该文件使用LXML整齐格式化并包含DTD(尚未实现)。我已经在我的应用程序的其他方面使用了LXML,所以我想我会继续使用它。我使用的是Python 3。

**更新2 **

  

控制台

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

文件

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

1 个答案:

答案 0 :(得分:1)

对于XML文件,只需使用'wb'中的open()参数并考虑上下文管理器with

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

作为演示,下面测试OP的例程,稍微更改 self 变量,并以漂亮的打印格式输出:

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

<强>输出

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