【发布时间】:2020-10-23 09:01:39
【问题描述】:
我在 Python 中使用 ElementTree 创建了一个 xml 文件。我对python很陌生,所以如果在术语上犯了一些错误,请原谅。 我想合并具有相同属性名称的元素的内容。
<?xml version="1.0" ?>
<DefaultLines>
<Files Date="2020-10-23" Name="D:\report_byfile_detailed.txt">
<FileName file="emem_fifo_1c.vhd ">
<DefLines>
<Message>'120'<Child>Statement</Child>w_addr <= (others => '0');</Message>
</DefLines>
<DefLines>
<Message>'136'<Child>Statement</Child>r_addr <= (others => '0');</Message>
</DefLines>
</FileName>
<FileName file="emem_fifo_1c.vhd ">
<DefLines>
<Message>'119'<Child>Branch</Child>if (SRESET = '1') then</Message>
</DefLines>
</FileName>
</Files>
</DefaultLines>
例如 Filename1 和 Filename2 具有相同的属性,即“emem_fifo_1c.vhd”。如果“文件”相同,我希望将 FileName 中的元素合并为一个。
我的输出 xml 应该是这样的
<?xml version="1.0" ?>
<DefaultLines>
<Files Date="2020-10-23" Name="D:\report_byfile_detailed.txt">
<FileName file="emem_fifo_1c.vhd ">
<DefLines>
<Message>'120'<Child>Statement</Child>w_addr <= (others => '0');</Message>
</DefLines>
<DefLines>
<Message>'136'<Child>Statement</Child>r_addr <= (others => '0');</Message>
</DefLines>
<DefLines>
<Message>'119'<Child>Branch</Child>if (SRESET = '1') then</Message>
</DefLines>
</FileName>
</Files>
</DefaultLines>
我真的不知道如何在 python 中使用 ElementTree。
更新: 我正准备在大兵搜的帮助下解决这个问题。但是,我面临节点内重复内容的另一个问题。我试图在将它们添加到 xml 时删除它们,但它不起作用。
<?xml version="1.0" ?>
<DefaultLines>
<Files Date="2020-10-31" Name="D:\report_byfile_detailed.txt">
<FileName file="emem_fifo_1c.vhd ">
<DefLines>
<Message>'108'<Child>Expression</Child>Item 1 ((W_EN and not(fifo_full)) and not(SRESET))</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>'119'<Child>Branch</Child>if (SRESET = '1') then</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>108<Child>Expression</Child>Item 1 ((W_EN and not(fifo_full)) and not(SRESET))</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>109<Child>Expression</Child>Item 1 ((R_EN and not(fifo_empty)) and not(SRESET))</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<DefLines>
<Message>108<Child>Expression</Child>Item 1 ((W_EN and not(fifo_full)) and not(SRESET))</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>Row 4:<Child>Expression</Child>fifo_full_1 not SRESET && W_EN</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>Row 6:<Child>Expression</Child>SRESET_1 (W_EN and not(fifo_full))</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>Row 4:<Child>Expression</Child>fifo_full_1 not SRESET && W_EN</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>Row 6:<Child>Expression</Child>SRESET_1 (W_EN and not(fifo_full))</Message>
<Justification />
<Comment />
<Status />
</DefLines>
'108', '109' 'Row4', Row6' 被多次附加。是否有可能我只保留第一次出现并删除其余的。
更新: 使用该方法删除重复项后,我得到的 xml 节点不完整:
<?xml version="1.0" ?>
<DefaultLines>
<Files Date="2020-11-01" Name="D:\report_byfile_detailed.txt">
<FileName file="emem_fifo_1c.vhd ">
<DefLines>
<Message>
'108'
<Child>Expression</Child>
Item 1 ((W_EN and not(fifo_full)) and not(SRESET))
</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>
'119'
<Child>Branch</Child>
if (SRESET = '1') then
</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>
'120'
<Child>Statement</Child>
w_addr <= (others => '0');
</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>
'135'
<Child>Branch</Child>
if (SRESET = '1') then
</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>
'136'
<Child>Statement</Child>
r_addr <= (others => '0');
</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>
'157'
<Child>Branch</Child>
if (SRESET = '1') then
</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>
'158'
<Child>Statement</Child>
fifo_empty <= '1';
</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>
'180'
<Child>Branch</Child>
if (SRESET = '1') then
</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>
'181', '182'
<Child>Statement</Child>
fifo_used <= (others => '0');
fifo_used_one <= '0';
</Message>
<Justification />
<Comment />
<Status />
</DefLines>
Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>
'568', '569', '570', '571'
<Child>Statement</Child>
config_rd_fsm
<= '0';
axi4_lite_slave_rdata_ch_out <= AXI4LITE_RDATA32_S2M_DEF;
config_rd_fsm <= IDLE;
</Message>
<Justification />
<Comment />
<Status />
**<
<DefLines>**
<Message>
161
<Child>Condition</Child>
Item 1 (((r_en_valid = '1') and (fifo_used_one = '1')) and (w_en_valid = '0'))
</Message>
<Justification />
<Comment />
<Status />
</DefLines>
DefLines>
<DefLines>
<Message>
367
<Child>Branch</Child>
when others =>
</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>
**<Child>Bran**
<DefLines>
<Message>
Row 5:
<Child>Condition</Child>
(w_en_valid = '0')_0 ((r_en_valid = '1') and (fifo_used_one = '1'))
</Message>
<Justification />
<Comment />
<Status />
**</DefLines>sh</Child>**
All False Count
</Message>
<Justification />
<Comment />
<Status />
</DefLines>
<DefLines>
<Message>
587
<Child>Branch</Child>
when others =>
</Message>
**<Justi>**
</FileName>
我尝试将树不完整的区域加粗,因此在生成和解析 xml 树时出现错误
【问题讨论】:
-
可以改用lxml库吗?这样就容易多了。
-
我的整个代码都是基于元素树的。如果您能对在这种情况下如何使用 lxml 有所了解,那将非常有帮助。感谢您的宝贵时间
标签: python xml elementtree