XML Etree 解析同名标签

XML Etree parse with tags named the same

全部,这几天一直在努力解决这个问题,虽然我认为我已经接近了,但它只是返回空白,因为它没有抓住正确的 XML。

样本XML

<Attribute>
     <Name>Column1</Name>
     <Value>abcded</Value>
</Attribute>
<Attribute>
    <Name>Column2</Name>
    <Value>abcdef</Value>
</Attribute>
<Attribute>
    <Name>coumn3</Name>
    <Value>abcdef</Value>
</Attribute>

代码

for node in parsed_xml.iter():
    Attributes = node.get.attrib('column1')
    correlationssnnamecount = node.find('column2')
    divphoneaddrcount = node.find('column3')

df_xml = df_xml.append(
    pd.Series([column1, getvalueofnode(column2),
               getvalueofnode(column3)], 
               index=dfcols),
               ignore_index=True)

print df_xml

我正在寻找的基本上是我的数据框,标题为 "column1",值 "column2",等等

由于文件已更改,这就是解决方案 但请注意,对于有效的 xml 文件,您需要类似

的内容
<data> ...</data> 

开始和结束

import xml.etree.ElementTree as ET
import pandas as pd

inp=ET.parse("inputfile.xml")
inroot=inp.getroot()
ss={}

for child in inroot.iter('Attribute'):
    for n,v in zip(child.findall(".//Name"),child.findall(".//Value")):
        ss.update({n.text:v.text})

df=pd.DataFrame(ss,index=[0])

Out:
   Column1 Column2  coumn3
0  abcded  abcdef  abcdef

如果您的 inputfile.xml 是这里给出的 xml 文件 https://docs.python.org/2/library/xml.etree.elementtree.html#module-xml.etree.ElementTree

那就是

import xml.etree.ElementTree as ET
import pandas as pd

inp=ET.parse("inputfile.xml")
inroot=inp.getroot()

df=pd.DataFrame(columns= ['Country','rank','year','gdppc'])

ranks = inroot.findall(".//rank")
years = inroot.findall(".//year")
gdppc= inroot.findall(".//gdppc")

df['Country']=[child.attrib['name'] for child in inroot]
df['rank']=[r.text for r in ranks]
df['year']=[y.text for y in years]
df['gdppc'] = [g.text for g in gdppc]

这会根据您的要求生成数据框。

df:

      Country     rank  year  gdppc
0  Liechtenstein    1  2008  141100
1      Singapore    4  2011   59900
2         Panama   68  2011   13600