なぜelement.find("..")
返信root
root.findall("*")
はelement
を返しますか? XMLファイルのpython xml - find( "..")がルートを返さない理由
def XML_Extract_Node_Tags(Tree, Node_Tags):
"""
:param Tree: xml.etree.ElementTree
:param Node_Tags: list
:return: ReturnVal:
"""
for el in Tree.findall("//"):
if el.tag not in Node_Tags:
print(el.tag)
# Need to remove the element and set its children equal to parent
for subel in el.findall("*"):
## Add subel to grandparent (if exists)
grand_parent = subel.find('../..')
if grand_parent:
# If it has a grand parent
grand_parent.append(subel)
# Remove el from tree
if not el.find(".."):
print(el.tag, el.attrib)
else:
el.find("..").remove(el)
ReturnVal = Tree
return ReturnVal
第5ライン
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE build SYSTEM "build.dtd">
<build id="58" localAgents="true" cm="usl000348:80" start="6/1/2016 3:31:19 PM">
<properties>
<property name="CommandLine">emake all --emake-annodetail=waiting,registry,md5,lookup,history,file,env --emake-annofile=../Emake-2agents-1st.xml --emake-root=../</property>
'el.find( '..')'は、この要素のすべての子要素で、親を見つけますか?要素の親ではなく、要素自体を指すのではないでしょうか? –
@JohnGordon touche。はい、あなたは正しい:[最初の**サブ要素**一致するマッチを検索する](https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.find ) – Adrian
も参照してください:http://stackoverflow.com/questions/2170610/access-elementtree-node-parent-node –