相关疑难解决方法(0)

访问ElementTree节点父节点

我正在使用内置的Python ElementTree模块.访问子节点很简单,但父节点或兄弟节点呢? - 这可以在不遍历整棵树的情况下有效地完成吗?

python elementtree

55
推荐指数
6
解决办法
5万
查看次数

在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)

有没有办法可以做到这一点?

谢谢

python elementtree

19
推荐指数
3
解决办法
4万
查看次数

标签 统计

elementtree ×2

python ×2