XML文件解析 - 从每个父级及其子级获取数据

时间:2015-06-19 16:16:40

标签: python xml python-3.x elementtree

我想从每个父级和他们自己的孩子那里获取XML文件的数据。

我试图解析这个XML文件

<DB>
    <Entry>
        <Name>Assembly.iam</Name>
        <DisplayName>Assembly.iam</DisplayName>
        <Scalar>
            <Name>d0</Name>
            <DisplayName>d0 (value = 0 mm)</DisplayName>
            <Value>0</Value>
        </Scalar>
        <Scalar>
             <Name>d1</Name>
             <DisplayName>d1 (value = 0 mm)</DisplayName>
        <Value>0</Value>
        </Scalar>
    </Entry>
    <Entry>
        <Name>Ground.ipt</Name>
        <DisplayName>Ground.ipt</DisplayName>
        <Scalar>
            <Name>Ground_length</Name>
            <DisplayName>Ground_length (value = 160 mm)</DisplayName>
            <Value>160</Value>
        </Scalar>
        <Scalar>
            <Name>d2</Name>
            <DisplayName>d2 (value = 80 mm)</DisplayName>
            <Value>80</Value>
        </Scalar>
    </Entry>
</DB>

事实上,我想获取<DisplayName></DisplayName>的数据。 然后,我想将这些数据放入像这样的元组数组

[(Assembly.iam,[d0 (value = 0 mm), d1 (value = 0 mm)]),
(Ground.ipt,[Ground_length (value = 160 mm), d2 (value = 80 mm)])

我尝试将xml.etree.cElementTree库与此代码一起使用

from xml.etree import cElementTree
import numpy as np

workingDir = "C:/Users/Vince/Test"
newStrWorkingDir = str.replace(workingDir, '/', '\\')
tree = cElementTree.parse(newStrWorkingDir + "\\test.xml")
root = tree.getroot()
tab = np.empty(shape=(0, 0))
tabEntry = np.empty(shape=(0, 0))
tabScalar = np.empty(shape=(0, 0))

for entry in root.findall('Entry'):
    entryNames = entry.findall("./DisplayName")
    entryNamesText = entry.find("./DisplayName").text
    tabEntry = np.append(tabEntry,entryNamesText)
    for scalar in entry.findall('Scalar'):
        scalarNames = scalar.findall("./DisplayName")
        scalarNamesText = scalar.find("./DisplayName").text
        tabScalar = np.append(tabScalar,scalarNamesText)
        tab = np.append(tab,(entryNamesText,scalarNamesText))

print(tab)

但它输出了我

['Assembly.iam' 'd0 (value = 0 mm)'
'Assembly.iam' 'd1 (value = 0 mm)'
'Ground.ipt' 'Ground_length (value = 160 mm)' 
'Ground.ipt' 'd2 (value = 80 mm)']

1 个答案:

答案 0 :(得分:1)

要获得您想要的结构,您必须构建列表列表:

import os
from xml.etree import cElementTree

workingDir = "C:\\Users\\Vince\\Test"
tree = cElementTree.parse(os.path.join(newStrWorkingDir, "test.xml"))
root = tree.getroot()
tab = []

for entry in root.findall('Entry'):
    entry_name = entry.findtext("./DisplayName")
    scalar_names = [e.text for e in entry.findall('Scalar/DisplayName')]
    tab.append((entry_name, scalar_names))
print(tab)