将XML解析为wx.Python RichTextCtrl

时间:2015-07-11 07:32:11

标签: xml wxpython richtextbox

我正在尝试在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()

有人可以告诉我哪里出错了吗?

2 个答案:

答案 0 :(得分:0)

代码中特定的哪一行会引发错误?

答案 1 :(得分:0)

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

来解决该问题