使用ElementTree解析任意XML文件
我有一个模板XML文件,并根据给我的程序的输入,我必须生成一个新的XML文件。该模板具有需要根据输入数据重复的部分。但我不一定知道这些部分的结构或他们有多少层次的嵌套。我无法弄清楚如何以任意方式读取模板文件,他们会让我填充它然后输出它。这里是模板文件的一部分:使用ElementTree解析任意XML文件
<Target_Table>
<Target_Name>SF1_T1</Target_Name>
<Target_Mode>
<REP>
<Target_Location_To_Repeat>
<XLocation>nextXREL</XLocation>
<YLocation>nextYREL</YLocation>
</Target_Location_To_Repeat>
<Target_Location_To_Repeat>
<XLocation>nextXREL</XLocation>
<YLocation>nextYREL</YLocation>
</Target_Location_To_Repeat>
</REP>
</Target_Mode>
<Target_Repetitions>1</Target_Repetitions>
<Meas_Window>
<Window_Size>
<XLocation>FOV</XLocation>
<YLocation>FOV</YLocation>
</Window_Size>
<Window_Location>
<XLocation>firstXREL</XLocation>
<YLocation>firstYREL</YLocation>
</Window_Location>
</Meas_Window>
<Box_Orientation>90</Box_Orientation>
<First_Feature Value="Space" />
<Meas_Params_Definition>
<Number_Of_Lines Value="Auto" />
<Number_Of_Pixels_Per_Line Value="Auto" />
<Averaging_Factor Value="1" />
</Meas_Params_Definition>
<Number_Of_Edges>1</Number_Of_Edges>
<Edge_Pair>
<Edge_Pair_Couple>
<First_Edge>1</First_Edge>
<Second_Edge>1</Second_Edge>
</Edge_Pair_Couple>
<Nominal_Corrected_Value>0</Nominal_Corrected_Value>
</Edge_Pair>
<Categories>
<Material_Type />
<Meas_Type />
<Category_Type />
<Other_Type />
</Categories>
<Bias>0</Bias>
<Template_Target_Name>SF_IMAQ_Template_Target</Template_Target_Name>
<Template_Target_PPL>
<Process>PC2</Process>
<Product>PD2</Product>
<Layer>L2</Layer>
</Template_Target_PPL>
<Meas_Auto_Box>
<Error_Code>0</Error_Code>
<Measured_CD>0</Measured_CD>
<Constant_NM2Pix>true</Constant_NM2Pix>
</Meas_Auto_Box>
<Meas_Box_Pix_Size_X>PixelSize</Meas_Box_Pix_Size_X>
<Macro_CD>0</Macro_CD>
</Target_Table>
我需要重复整个Target_Table节多时间,和每一个Target_Table内我需要多次重复REP部分。我想编写我的程序,以便如果模板发生变化(例如,添加更多层次的嵌套),我不必更改程序。但在我看来,我必须完全知道文件的结构才能读取并吐出。这是真的还是我错过了什么?有没有办法编写一个程序读入一个未知标签和未知嵌套层次的文件?
使用ElementTree的:
import xml.etree.ElementTree as et
filehandler = open("file.xml","r")
raw_data = et.parse(filehandler)
data_root = raw_data.getroot()
filehandler.close()
for children in data_root:
for child in children:
print(child.tag, child.text, children.tag, children.text)
这会给你的XML标签的概述以及内部相关文本标签。 您可以添加更多循环以进一步进入树中,并执行检查以查看是否有任何子级包含更多级别。当XML标签的名称变化并且不遵循已知标准时,我发现这种方法很有用。
一个例子using BeautifulSoup:
import sys
from bs4 import BeautifulSoup
file = sys.argv[1]
handler = open(file).read()
soup = BeautifulSoup(handler)
for table in soup.find_all("target_table"):
for loc in table.find_all("rep"):
print loc.xlocation.string + ", " + loc.ylocation.string
输出
nextXREL, nextYREL
我宁愿不必使用外部模块。但我确实想尝试这个,但我无法安装它。我在运行python 2.6的Mac上。随着点我有一堆语法错误,然后它说它已成功安装(但事实并非如此)。通过easy_install,我得到了pkg_resources.DistributionNotFound。 – 2013-03-20 00:40:23
检查了这一点:http://stackoverflow.com/a/7110428/390913 – perreal 2013-03-20 00:41:32
我得到了相同的DistributionNotFound错误尝试。但如果可能的话,我真的更喜欢用ElementTree来解决这个问题。 – 2013-03-20 00:47:14
谢谢 - 这是非常有用的 - 仍然,你需要知道有多少关卡。我无法想出一个通用的方法来将树下降到最低。此外,我必须插入新的节点,并不清楚如何做到这一点。我想在一天结束的时候,自己解析文件会更容易。 – 2013-03-20 11:22:58
我认为'对于根中的孩子''应该这样写:'对于data_root中的孩子:' – emh 2014-03-04 15:30:12
你是绝对正确的,是固定的。 – 2014-05-03 23:06:20