我想将富文本控件中的xml字符串转换为字符串变量(存储在数据库中)。我可以保存到文件,然后将其作为字符串读回来,但必须有更好的方法(缓冲区?)。
我试过
buffer = self.rtc.GetBuffer()
但是如何从缓冲区中获取字符串?
有人可以帮助我。我搜索了文档和示例,但找不到我需要的内容。
答案 0 :(得分:0)
我会使用StringIO
将数据写入,然后将其打印出来。如果你这样做,那么你只需要写入内存,从磁盘读取速度要快得多。我找到了example,它完成了你想要的大部分内容,但它里面有很多额外的垃圾,所以我把这个例子减少到以下几点:
import wx
import wx.richtext
from StringIO import StringIO
########################################################################
class MyFrame(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, title='Richtext Test')
sizer = wx.BoxSizer(wx.VERTICAL)
self.rt = wx.richtext.RichTextCtrl(self)
self.rt.SetMinSize((300,200))
save_button = wx.Button(self, label="Save")
save_button.Bind(wx.EVT_BUTTON, self.on_save)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.rt, 1, wx.EXPAND|wx.ALL, 6)
sizer.Add(save_button, 0, wx.EXPAND|wx.ALL, 6)
self.SetSizer(sizer)
self.Show()
#----------------------------------------------------------------------
def on_save(self, event):
out = StringIO()
handler = wx.richtext.RichTextXMLHandler()
rt_buffer = self.rt.GetBuffer()
handler.SaveStream(rt_buffer, out)
out.seek(0)
self.xml_content = out.read()
print self.xml_content
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
更新 - 为了好玩,我决定扩展这个示例,让它将XML写入文件,然后将其读回并解析输入的任何文本:
import wx
import wx.richtext
from lxml import objectify
from StringIO import StringIO
########################################################################
class MyFrame(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, title='Richtext Test')
sizer = wx.BoxSizer(wx.VERTICAL)
self.rt = wx.richtext.RichTextCtrl(self)
self.rt.SetMinSize((300,200))
save_button = wx.Button(self, label="Save")
save_button.Bind(wx.EVT_BUTTON, self.on_save)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.rt, 1, wx.EXPAND|wx.ALL, 6)
sizer.Add(save_button, 0, wx.EXPAND|wx.ALL, 6)
self.SetSizer(sizer)
self.Show()
def on_save(self, event):
out = StringIO()
handler = wx.richtext.RichTextXMLHandler()
rt_buffer = self.rt.GetBuffer()
handler.SaveStream(rt_buffer, out)
out.seek(0)
self.xml_content = out.read()
print self.xml_content
with open("test.xml", "w") as xml_file:
xml_file.write(self.xml_content)
self.parse_xml()
def parse_xml(self):
'''
Parse the XML with the lxml module
'''
with open("test.xml") as xml_file:
xml = xml_file.read()
root = objectify.fromstring(xml)
print root.paragraphlayout.paragraph.getchildren()[0]
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
请注意,lxml模块不是Python的一部分,但可以轻松获取here
以下是我运行此程序时获得的一些示例XML:
<?xml version="1.0" encoding="UTF-8"?>
<richtext version="1.0.0.0" xmlns="http://www.wxwidgets.org">
<paragraphlayout textcolor="#3C3C3C" fontsize="10" fontstyle="90" fontweight="90" fontunderlined="0" fontface="Droid Sans" alignment="1" parspacingafter="10" parspacingbefore="0" linespacing="10">
<paragraph>
<text>glgfh</text>
</paragraph>
</paragraphlayout>
</richtext>
答案 1 :(得分:-1)
这是对上述内容的补充:
我已根据需要保存了xml,但当我读回来时,我收到一个错误:
XML parsing error: 'no element found' at line 1
我的代码是
def OnLstStrategyDClick(self,e):
index=e.GetSelection()
self.txtStrategyID.SetValue(self.StrategyList[index][0])
self.txtDescription.SetValue(self.StrategyList[index][1])
#print self.StrategyList[index][2]
out = StringIO()
handler = wx.richtext.RichTextXMLHandler()
buffer = self.txtBidStrategy.GetBuffer()
buffer.AddHandler(handler)
out.write(self.StrategyList[index][2])
out.seek(0)
handler.LoadStream(buffer, out)
self.txtBidStrategy.Refresh()
self.txtBidStrategy.SetValue(self.StrategyList[index][2])
xml代码看起来很好(好吧,它已由处理程序生成)