Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -*- coding: utf-8 -*- 

2from minidom import Document 

3import copy 

4 

5 

6class dict2xml(object): 

7 doc = Document() 

8 def __init__(self, structure): 

9 if len(structure) == 1: 

10 rootName = str(structure.keys()[0]) 

11 self.root = self.doc.createElement(rootName) 

12 self.doc.toprettyxml(indent=" ") 

13 self.doc.appendChild(self.root) 

14 self.build(self.root, structure[rootName]) 

15 

16 def build(self, father, structure): 

17 if type(structure) == dict: 

18 for k in structure: 

19 tag = self.doc.createElement(k) 

20 father.appendChild(tag) 

21 self.build(tag, structure[k]) 

22 

23 elif type(structure) == list: 

24 grandFather = father.parentNode 

25 tagName = father.tagName 

26 grandFather.removeChild(father) 

27 for l in structure: 

28 tag = self.doc.createElement(tagName) 

29 self.build(tag, l) 

30 grandFather.appendChild(tag) 

31 

32 else: 

33 data = str(structure) 

34 tag = self.doc.createTextNode(data) 

35 father.appendChild(tag) 

36 

37 def display(self): 

38 test = self.doc.toprettyxml(indent=" ") 

39 return test 

40 

41 # # Xml 딕셔너리로 주는거 같은데 

42 # def dictify(r, root=True): 

43 # if root: 

44 # return {r.tag: dictify(r, False)} 

45 # d = copy(r.attrib) 

46 # if r.text: 

47 # d["_text"] = r.text 

48 # for x in r.findall("./*"): 

49 # if x.tag not in d: 

50 # d[x.tag] = [] 

51 # d[x.tag].append(dictify(x, False)) 

52 # return d