我正在使用内置的Python ElementTree模块.访问子节点很简单,但父节点或兄弟节点呢? - 这可以在不遍历整棵树的情况下有效地完成吗?
我有一个XML文档,我想在其中搜索一些元素,如果它们符合某些标准,我想删除它们
但是,我似乎无法访问元素的父级,以便我可以删除它
file = open('test.xml', "r")
elem = ElementTree.parse(file)
namespace = "{http://somens}"
props = elem.findall('.//{0}prop'.format(namespace))
for prop in props:
type = prop.attrib.get('type', None)
if type == 'json':
value = json.loads(prop.attrib['value'])
if value['name'] == 'Page1.Button1':
#here I need to access the parent of prop
# in order to delete the prop
Run Code Online (Sandbox Code Playgroud)
有没有办法可以做到这一点?
谢谢