擅长:python、mysql、java
<p>上面提到的问题,我可以为其他人找到答案:</p>
<pre><code> xml_data = open('test.xml').read().encode('utf8')
def xml2df(xml_data ):
tree = et.parse(xml_data )
all_records= []
result= {}
for el in tree.iterfind("./row:agent1/row:agent2/row3:agent3/xs:top_col/",namespaces):
for r in el:
if '}' in r.tag:
r.tag = r.tag.split('}', 1)[1]
for i in el.iterfind('*'):
for s in i:
s.tag = s.tag.split('}',1)[1]
s.tag = i.tag +"."+s.tag
result[i.tag] = i.text
for j in i.iterfind('*'):
result[j.tag] = j.text
all_records.append(result)
result= {}
df = pd.DataFrame(data)
return df
df1 = xml2df(xml_data)
df1
</code></pre>