Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ElementTree XML API与子元素不匹配_Python_Xml_Parsing_Elementtree - Fatal编程技术网

Python ElementTree XML API与子元素不匹配

Python ElementTree XML API与子元素不匹配,python,xml,parsing,elementtree,Python,Xml,Parsing,Elementtree,我正在尝试使用USPS API返回包跟踪的状态。我有一个返回ElementTree.Element对象的方法,该对象是根据USPSAPI返回的XML字符串构建的 这是返回的XML字符串 <?xml version="1.0" encoding="UTF-8"?> <TrackResponse> <TrackInfo ID="EJ958088694US"> <TrackSummary>The Postal Service co

我正在尝试使用USPS API返回包跟踪的状态。我有一个返回ElementTree.Element对象的方法,该对象是根据USPSAPI返回的XML字符串构建的

这是返回的XML字符串

<?xml version="1.0" encoding="UTF-8"?>
  <TrackResponse>
    <TrackInfo ID="EJ958088694US">
      <TrackSummary>The Postal Service could not locate the tracking information for your 
       request. Please verify your tracking number and try again later.</TrackSummary>
    </TrackInfo>
  </TrackResponse>
现在我可以在xml字符串中看到标记“TrackSummary”存在,我希望能够使用ElementTree的find方法访问它

作为额外的证明,我可以迭代响应对象并证明“TrackSummary”标记存在

for item in response.iter():
    print(item, item.text)
返回:

<Element 'TrackResponse' at 0x00000000041B4B38> None
<Element 'TrackInfo' at 0x00000000041B4AE8> None
<Element 'TrackSummary' at 0x00000000041B4B88> The Postal Service could not locate the tracking information for your request. Please verify your tracking number and try again later.
返回

None
我是不是遗漏了什么?看起来我应该可以毫无问题地找到子元素

方法
.find()
只搜索下一层,而不是递归搜索。要递归搜索,需要使用XPath查询。在XPath中,双斜杠
/
是一种递归搜索。试试这个:

# returns a list of elements with tag TrackSummary
response.xpath('//TrackSummary')

# returns a list of the text contained in each TrackSummary tag
response.xpath('//TrackSummary/node()')
.find()
方法只搜索下一层,而不是递归。要递归搜索,需要使用XPath查询。在XPath中,双斜杠
/
是一种递归搜索。试试这个:

# returns a list of elements with tag TrackSummary
response.xpath('//TrackSummary')

# returns a list of the text contained in each TrackSummary tag
response.xpath('//TrackSummary/node()')
选择所有子元素。例如,*/egg选择所有名为egg的孙子

element = response.findall('*/TrackSummary') # you will get a list
print element[0].text #fast print else iterate the list

>>> The Postal Service could not locate the tracking informationfor your request. Please verify your tracking number and try again later.
选择所有子元素。例如,*/egg选择所有名为egg的孙子

element = response.findall('*/TrackSummary') # you will get a list
print element[0].text #fast print else iterate the list

>>> The Postal Service could not locate the tracking informationfor your request. Please verify your tracking number and try again later.
element = response.findall('*/TrackSummary') # you will get a list
print element[0].text #fast print else iterate the list

>>> The Postal Service could not locate the tracking informationfor your request. Please verify your tracking number and try again later.