1+ #!/usr/bin/env python
2+ # encoding: utf-8
3+
4+ """
5+ @author: zhanghe
6+ @software: PyCharm
7+ @file: test_xmltodict.py
8+ @time: 2016/10/10 下午11:25
9+ """
10+
11+
12+ import xmltodict
13+ import json
14+ from copy import deepcopy
15+
16+
17+ xml_str_test = """
18+ <data>
19+ <country>
20+ <rank>1</rank>
21+ <year>2008</year>
22+ <gdppc>141100</gdppc>
23+ </country>
24+ <country>
25+ <rank>4</rank>
26+ <year>2011</year>
27+ <gdppc>59900</gdppc>
28+ </country>
29+ <country>
30+ <rank>68</rank>
31+ <year>2011</year>
32+ <gdppc>13600</gdppc>
33+ <items>
34+ <i>1</i>
35+ <i>2</i>
36+ <i></i>
37+ </items>
38+ </country>
39+ </data>
40+ """
41+
42+
43+ def xml_to_dict (xml_str ):
44+ """
45+ 将xml转为dict
46+ :param xml_str:
47+ :return:
48+ """
49+ xml_dict = xmltodict .parse (xml_str )
50+ print json .dumps (xml_dict , indent = 4 )
51+ output (json .loads (json .dumps (xml_dict )))
52+
53+
54+ def output (data , row = None ):
55+ """
56+ 递归所有分支
57+ :param data:
58+ :param row:
59+ :return:
60+ """
61+ row_list = row if row else []
62+ if isinstance (data , list ):
63+ for index , item in enumerate (data ):
64+ list1 = deepcopy (row_list )
65+ list1 .append ('>' )
66+ list1 .append ('[%s]' % index )
67+ output (item , list1 )
68+ if isinstance (data , dict ):
69+ for key , value in data .items ():
70+ list2 = deepcopy (row_list )
71+ if list2 :
72+ list2 .append ('>' )
73+ list2 .append (key )
74+ output (value , list2 )
75+ if isinstance (data , str ) or isinstance (data , unicode ) or data is None :
76+ row_list .append (':' )
77+ row_list .append (data if data else '' )
78+ print ' ' .join (row_list )
79+
80+
81+ def run ():
82+ xml_to_dict (xml_str_test )
83+
84+
85+ if __name__ == '__main__' :
86+ run ()
87+
88+
89+ # pip install xmltodict
90+
91+ """
92+
93+ {
94+ "data": {
95+ "country": [
96+ {
97+ "rank": "1",
98+ "year": "2008",
99+ "gdppc": "141100"
100+ },
101+ {
102+ "rank": "4",
103+ "year": "2011",
104+ "gdppc": "59900"
105+ },
106+ {
107+ "rank": "68",
108+ "year": "2011",
109+ "gdppc": "13600",
110+ "items": {
111+ "i": [
112+ "1",
113+ "2"
114+ ]
115+ }
116+ }
117+ ]
118+ }
119+ }
120+ data > country > [0] > gdppc : 141100
121+ data > country > [0] > rank : 1
122+ data > country > [0] > year : 2008
123+ data > country > [1] > gdppc : 59900
124+ data > country > [1] > rank : 4
125+ data > country > [1] > year : 2011
126+ data > country > [2] > items > i > [0] : 1
127+ data > country > [2] > items > i > [1] : 2
128+ data > country > [2] > items > i > [2] :
129+ data > country > [2] > gdppc : 13600
130+ data > country > [2] > rank : 68
131+ data > country > [2] > year : 2011
132+
133+ """
0 commit comments