Skip to content

Commit 42d677e

Browse files
committed
新增xml解析
1 parent 0c700af commit 42d677e

2 files changed

Lines changed: 203 additions & 0 deletions

File tree

test/test_etree.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""
5+
@author: zhanghe
6+
@software: PyCharm
7+
@file: 23432.py
8+
@time: 2016/10/10 下午10:43
9+
"""
10+
11+
12+
import xml.etree.ElementTree as ET
13+
14+
15+
xml_str_test = """
16+
<data>
17+
<country name="Liechtenstein">
18+
<rank>1</rank>
19+
<year>2008</year>
20+
<gdppc>141100</gdppc>
21+
<neighbor name="Austria" direction="E"/>
22+
<neighbor name="Switzerland" direction="W"/>
23+
</country>
24+
<country name="Singapore">
25+
<rank>4</rank>
26+
<year>2011</year>
27+
<gdppc>59900</gdppc>
28+
<neighbor name="Malaysia" direction="N"/>
29+
</country>
30+
<country name="Panama">
31+
<rank>68</rank>
32+
<year>2011</year>
33+
<gdppc>13600</gdppc>
34+
<neighbor name="Costa Rica" direction="W"/>
35+
<neighbor name="Colombia" direction="E"/>
36+
</country>
37+
</data>
38+
"""
39+
40+
41+
def print_node(node):
42+
# print "attrib:%s" % node.attrib
43+
print "tag:%s" % node.tag
44+
print "text:%s" % node.text
45+
46+
47+
def read_nodes(em):
48+
print type(em), em
49+
for child in em:
50+
print_node(child)
51+
read_nodes(child)
52+
53+
54+
def xml_to_dict(xml_str):
55+
"""
56+
将xml转为dict
57+
"""
58+
dict_data = {}
59+
root = ET.fromstring(xml_str)
60+
dict_data[root.tag] = {}
61+
print_node(root)
62+
read_nodes(root)
63+
64+
65+
def run():
66+
xml_to_dict(xml_str_test)
67+
68+
69+
if __name__ == '__main__':
70+
run()

test/test_xmltodict.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

Comments
 (0)