用Python读取XML文件

时间:2014-05-06 16:42:26

标签: python xml elementtree

我在Python中使用ElementTree来读取XML文件并获取一些值。问题是我成功获得了变量名称,但是我无法读取包含的值,例如,在DEFINITION标记下的TABLE标记中。

这是我读取名称的代码:

tree = parse("basicfirealarm.xml")
root = tree.find('NETWORK')

for node in root:
    var = node.find('NAME')        # access the var definition
    print var.text

这是我正在尝试阅读的文件,您能否给我一些建议如何使用ElementTree访问TABLE标记中的值?

谢谢

<?xml version="1.0" encoding="US-ASCII"?>

<!-- DTD for the XMLBIF 0.3 format -->
<!DOCTYPE BIF [
<!ELEMENT BIF ( NETWORK )*>
   <!ATTLIST BIF VERSION CDATA #REQUIRED>
<!ELEMENT NETWORK ( NAME, ( PROPERTY | VARIABLE | DEFINITION )* )>
<!ELEMENT NAME (#PCDATA)>
<!ELEMENT VARIABLE ( NAME, ( OUTCOME |  PROPERTY )* ) >
<!ATTLIST VARIABLE TYPE (nature|decision|utility) "nature">
<!ELEMENT OUTCOME (#PCDATA)>
<!ELEMENT DEFINITION ( FOR | GIVEN | TABLE | PROPERTY )* >
<!ELEMENT FOR (#PCDATA)>
<!ELEMENT GIVEN (#PCDATA)>
<!ELEMENT TABLE (#PCDATA)>
<!ELEMENT PROPERTY (#PCDATA)>
]>

<BIF VERSION="0.3">
<NETWORK>

<VARIABLE TYPE="nature">
   <NAME>tampering</NAME>
   <OUTCOME>T</OUTCOME>
   <OUTCOME>F</OUTCOME>
   <PROPERTY>position = (-148.1863, -197.12207)</PROPERTY>
</VARIABLE>

<VARIABLE TYPE="nature">
   <NAME>fire</NAME>
   <OUTCOME>T</OUTCOME>
   <OUTCOME>F</OUTCOME>
   <PROPERTY>position = (67.29127, -200.31433)</PROPERTY>
</VARIABLE>

<VARIABLE TYPE="nature">
   <NAME>alarm</NAME>
   <OUTCOME>T</OUTCOME>
   <OUTCOME>F</OUTCOME>
   <PROPERTY>position = (-46.03397, -61.451008)</PROPERTY>
</VARIABLE>

<VARIABLE TYPE="nature">
   <NAME>smoke</NAME>
   <OUTCOME>T</OUTCOME>
   <OUTCOME>F</OUTCOME>
   <PROPERTY>position = (158.27069, -67.83553)</PROPERTY>
</VARIABLE>

<VARIABLE TYPE="nature">
   <NAME>leaving</NAME>
   <OUTCOME>T</OUTCOME>
   <OUTCOME>F</OUTCOME>
   <PROPERTY>position = (-44.437843, 74.22005)</PROPERTY>
</VARIABLE>

<VARIABLE TYPE="nature">
   <NAME>report</NAME>
   <OUTCOME>T</OUTCOME>
   <OUTCOME>F</OUTCOME>
   <PROPERTY>position = (-42.841713, 200.31433)</PROPERTY>
</VARIABLE>


<DEFINITION>
   <FOR>tampering</FOR>
   <TABLE> 0.02 0.98</TABLE>
</DEFINITION>

<DEFINITION>b
   <FOR>fire</FOR>
   <TABLE> 0.01 0.99</TABLE>
</DEFINITION>

<DEFINITION>
   <FOR>alarm</FOR>
   <GIVEN>tampering</GIVEN>
   <GIVEN>fire</GIVEN>
   <TABLE> 0.5 0.5 0.85 0.15 0.99 0.01 1.0E-4 0.9999</TABLE>
</DEFINITION>

<DEFINITION>
   <FOR>smoke</FOR>
   <GIVEN>fire</GIVEN>
   <TABLE> 0.9 0.1 0.01 0.99</TABLE>
</DEFINITION>

<DEFINITION>
   <FOR>leaving</FOR>
   <GIVEN>alarm</GIVEN>
   <TABLE> 0.88 0.12 0.0010 0.999</TABLE>
</DEFINITION>

<DEFINITION>
   <FOR>report</FOR>
   <GIVEN>leaving</GIVEN>
   <TABLE> 0.75 0.25 0.01 0.99</TABLE>
</DEFINITION>


</NETWORK>
</BIF>

1 个答案:

答案 0 :(得分:1)

您需要将findall()'DEFINITION'一起使用,然后使用其下的'TABLE'标记:

>>> root = tree.find('NETWORK')
>>> 
>>> for node in root.findall('DEFINITION'):
...   print node.find('TABLE').text
... 
 0.02 0.98
 0.01 0.99
 0.5 0.5 0.85 0.15 0.99 0.01 1.0E-4 0.9999
 0.9 0.1 0.01 0.99
 0.88 0.12 0.0010 0.999
 0.75 0.25 0.01 0.99

如果你想要VARIABLE名称和值:

>>> for node in root.findall('DEFINITION'):
...     for child in node:
...         if child.tag in ('FOR', 'TABLE'):
...             print child.tag, '=', child.text 
... 
FOR = tampering
TABLE =  0.02 0.98
FOR = fire
TABLE =  0.01 0.99
FOR = alarm
TABLE =  0.5 0.5 0.85 0.15 0.99 0.01 1.0E-4 0.9999
# etc.