正在将 XML 解析为 wx.Python RichTextCtrl

Parsing XML into wx.Python RichTextCtrl

我试图在 wx.Python RichTextCtrl 中显示一些文本,但出现错误:

XML parsing error: 'not well-formed (invalid token)' at line 1

具有讽刺意味的是,xml 是由 RichTextXMLHandler 生成的。我的代码是:

import wx
import wx.richtext as rt
from StringIO import StringIO
from lxml import objectify

class DisplayXML(wx.Frame):

  def __init__(self, parent):
    super(DisplayXML, self).__init__(parent)

    self.InitUI()
    self.SetTitle('Display XML')
    self.Layout()
    self.Fit()
    self.Center()
    self.Show()
    self.DisplayRTF()

  def InitUI(self):
    self.txtRTF=rt.RichTextCtrl(self, size=(650,275), style=wx.VSCROLL|wx.HSCROLL);
  mainSizer=wx.BoxSizer(wx.HORIZONTAL)
  mainSizer.Add(self.txtRTF)
  self.SetSizer(mainSizer)
  self.InputXML='<?xml version="1.0" encoding="UTF-8"?>' + \
                  '<richtext version="1.0.0.0" xmlns="http://www.wxwidgets.org">' + \
                    '<paragraphlayout textcolor="#4C4C4C" fontsize="11" fontstyle="90"' + \
                        'fontweight="90" fontunderlined="0" fontface="Ubuntu" alignment="1"' + \
                        'parspacingafter="10" parspacingbefore="0" linespacing="10">' + \
                            '<paragraph alignment="2">' + \
                              '<text>Centred text</text>' + \
                      '</paragraph>' + \
                    '</paragraphlayout>' + \
                  '</richtext>'

  def DisplayRTF(self):
    rtfXML=self.InputXML

    #print rtfXML
    #root = objectify.fromstring(rtfXML)
    #print root.paragraphlayout.paragraph.getchildren()[0]

    out = StringIO()
    handler=wx.richtext.RichTextXMLHandler()
    buffer=self.txtRTF.GetBuffer()
    buffer.AddHandler(handler)
    out.write(rtfXML)
    out.seek(0)
    handler.LoadStream(buffer, out)
    self.txtRTF.Refresh()

    self.txtRTF.SetValue(self.InputXML)

if __name__ == '__main__':

  DisplayXMLApp=wx.App()
  DisplayXML(None)
  DisplayXMLApp.MainLoop()

谁能告诉我哪里出错了?

您的代码中具体是哪一行引发了错误?

我错了。通过确保将 xml 代码转换为字符串数据类型

,问题已得到解决