测试xml.etree.ElementTree的等效项

编程入门 行业动态 更新时间:2024-10-19 14:42:22
本文介绍了测试xml.etree.ElementTree的等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对两个xml元素的等效性感兴趣;而且我发现测试元素的字符串是可行的;

I'm interested in equivalence of two xml elements; and I've found that testing the tostring of the elements works; however, that seems hacky.

有没有更好的方法来测试两个etree元素的等效性?

Is there a better way to test equivalence of two etree Elements?

直接比较元素:

import xml.etree.ElementTree as etree h1 = etree.Element('hat',{'color':'red'}) h2 = etree.Element('hat',{'color':'red'}) h1 == h2 # False

将元素比较为字符串:

etree.tostring(h1) == etree.tostring(h2) # True

推荐答案

此比较功能对我有用:

def elements_equal(e1, e2): if e1.tag != e2.tag: return False if e1.text != e2.text: return False if e1.tail != e2.tail: return False if e1.attrib != e2.attrib: return False if len(e1) != len(e2): return False return all(elements_equal(c1, c2) for c1, c2 in zip(e1, e2))

更多推荐

测试xml.etree.ElementTree的等效项

本文发布于:2023-11-30 04:22:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648706.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   xml   ElementTree   etree

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!