]>
Commit | Line | Data |
---|---|---|
8bf5d46e | 1 | |
c368d904 RD |
2 | import string, sys |
3 | ||
4 | py2 = sys.version[0] == '2' | |
8bf5d46e RD |
5 | |
6 | from wxPython.wx import * | |
7 | try: | |
c368d904 RD |
8 | if py2: |
9 | from xml.parsers import expat | |
10 | parsermodule = expat | |
11 | else: | |
12 | from xml.parsers import pyexpat | |
13 | parsermodule = pyexpat | |
8bf5d46e RD |
14 | haveXML = true |
15 | except ImportError: | |
16 | haveXML = false | |
17 | ||
18 | #---------------------------------------------------------------------- | |
19 | ||
20 | if not haveXML: | |
21 | def runTest(frame, nb, log): | |
c368d904 RD |
22 | dlg = wxMessageDialog(frame, 'This demo requires the XML package. ' |
23 | 'See http://www.python.org/sigs/xml-sig/', | |
8bf5d46e RD |
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 ): | |
c368d904 RD |
37 | if py2: |
38 | name = name.encode() | |
9d8bd15f RD |
39 | id = self.AppendItem(self.nodeStack[-1], name) |
40 | self.nodeStack.append(id) | |
8bf5d46e RD |
41 | |
42 | def EndElement(self, name ): | |
43 | self.nodeStack = self.nodeStack[:-1] | |
44 | ||
45 | def CharacterData(self, data ): | |
46 | if string.strip(data): | |
c368d904 RD |
47 | if py2: |
48 | data = data.encode() | |
8bf5d46e RD |
49 | self.AppendItem(self.nodeStack[-1], data) |
50 | ||
51 | ||
52 | def LoadTree(self, filename): | |
53 | # Create a parser | |
c368d904 | 54 | Parser = parsermodule.ParserCreate() |
8bf5d46e RD |
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 |