刪除元素我有,我想尋找一些元素的XML文檔,如果它們與某些條件匹配 我想刪除這些搜索和與ElementTree的Python中
不過,我似乎無法能夠訪問該元素的父級,以便我可以刪除它
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
有沒有辦法我可以做到這一點?
感謝
我明白了。我也看看我讀過的lxml提供訪問元素的父項。無論如何,謝謝 – Thomas
是的,這是正確的。 lxml提供了一個'ElementTree'實現,它具有比接口通常狀態更多的特性。 lxml中的'Element'類提供'getparent()'方法來獲得對父元素的引用。 – Constantinius
如果子元素超過根元素的一個元素,該怎麼辦?如果它在不同的深度? – dwjohnston