将以下文本加载到ElementTree元素中时,find方法无法找到没有分配名称空间的元素之一。
import xml.etree.ElementTree as ElementTree
xml_text = """
<ns0:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/"><ns0:Body><ns0:Fault><faultcode>a:ActionNotSupported</faultcode><faultstring xml:lang="en-US">The message with Action \'\' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).</faultstring></ns0:Fault></ns0:Body></ns0:Envelope>
"""
xml = ElementTree.fromstring(xml_text)
ele = xml.find('faultstring')
ele == None #True
如何找到Faultstring元素?
答案 0 :(得分:2)
因为您只在寻找faultstring
,它是根元素Envelope
的直接子元素:
ele = xml.find('faultstring')
要在给定元素上下文中随处查找元素 ,可以使用.//element_name
:
ele = xml.find('.//faultstring')