]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/XMLtreeview.py
Change demo to not try to drop the table when it thinks it is creating it for the...
[wxWidgets.git] / wxPython / demo / XMLtreeview.py
1
2 import string, sys
3
4 py2 = sys.version[0] == '2'
5
6 from wxPython.wx import *
7 try:
8 if py2:
9 from xml.parsers import expat
10 parsermodule = expat
11 else:
12 from xml.parsers import pyexpat
13 parsermodule = pyexpat
14 haveXML = true
15 except ImportError:
16 haveXML = false
17
18 #----------------------------------------------------------------------
19
20 if not haveXML:
21 def runTest(frame, nb, log):
22 dlg = wxMessageDialog(frame, 'This demo requires the XML package. '
23 'See http://www.python.org/sigs/xml-sig/',
24 'Sorry', wxOK | wxICON_INFORMATION)
25 dlg.ShowModal()
26 dlg.Destroy()
27
28 else:
29
30 class XMLTree(wxTreeCtrl):
31 def __init__(self, parent, ID):
32 wxTreeCtrl.__init__(self, parent, ID)
33 self.nodeStack = [self.AddRoot("Root")]
34
35 # Define a handler for start element events
36 def StartElement(self, name, attrs ):
37 if py2:
38 name = name.encode()
39 id = self.AppendItem(self.nodeStack[-1], name)
40 self.nodeStack.append(id)
41
42 def EndElement(self, name ):
43 self.nodeStack = self.nodeStack[:-1]
44
45 def CharacterData(self, data ):
46 if string.strip(data):
47 if py2:
48 data = data.encode()
49 self.AppendItem(self.nodeStack[-1], data)
50
51
52 def LoadTree(self, filename):
53 # Create a parser
54 Parser = parsermodule.ParserCreate()
55
56 # Tell the parser what the start element handler is
57 Parser.StartElementHandler = self.StartElement
58 Parser.EndElementHandler = self.EndElement
59 Parser.CharacterDataHandler = self.CharacterData
60
61 # Parse the XML File
62 ParserStatus = Parser.Parse(open(filename,'r').read(), 1)
63
64
65 def runTest(frame, nb, log):
66 win = XMLTree(nb, -1)
67 win.LoadTree("paper.xml")
68 return win
69
70 #----------------------------------------------------------------------
71
72
73
74
75
76
77 overview = """\
78 """
79