如何从 xml 文件中提取特定内容,其中包含命名空间,从 API 收集?下面的细节是

How to extract the specific content from the xml file with namespace in it, collected from an API? The below details are

<?xml version="1.0" encoding="UTF-8"?>
<service xmlns="http://www.w3.org/2005/app" xmlns:app="http://www.w3.org/2005/app" xmlns:atom="http://www.w3.org/2005/Atom" xml:base="https://api*.successfactors.com/odata/v2/">
    <workspace>
        <atom:title>Default</atom:title>
        <collection href="User">
            <atom:title>User</atom:title>
        </collection>
        <collection href="FOLegalEntityLocalUSA">
            <atom:title>FOLegalEntityLocalUSA</atom:title>
        </collection>
        <collection href="DGFieldValue">
            <atom:title>DGFieldValue</atom:title>
        </collection>
        <collection href="NameFormatGO">
            <atom:title>NameFormatGO</atom:title>
        </collection>
    </workspace>
</service>

我想提取 .

的值(用户、FOLegalEntityLocalUSA 等)
from xml.etree.ElementTree as ET.
...
...
response = requests.get(url, auth=auth_values)
print (response.text)
doc = ET.fromstring(response.content)
namespaces= {'web':'http://www.w3.org/2005/app','atm':'http:   
//www.w3.org/2005/Atom'}
for work in doc.findall('web:workspace',namespaces):
   coll = work.find('web:collection',namespaces)
   for a in coll.findall('atm:title',namespaces):
      print (a.text)

它现在只打印第一个单个值 (atom:title) "User"。不知道如何循环获取所有 (atom:title) 值。

我已经尝试了所有可能的方法来提取标题。到目前为止没有运气。我目前被困住了,不知道该怎么办。请帮我。谢谢

您的 XML 示例似乎缺少 atom 命名空间前缀的命名空间声明。

我猜你在问题中显示的XML内容不完整? (现在问题已经更新,更正XML内容

假设某处有一个 xmlns:atom="http://www.w3.org/2005/Atom" 声明,获取 atom:title 元素的代码应该是:

for collection in d.findall('collection'):
    r = title.find('{http://www.w3.org/2005/Atom}title').text
    # or
    r = title.find('atom:title', namespaces={'atom': 'http://www.w3.org/2005/Atom'})

更新:

这就是您获得所有头衔的方式:

namespaces= {'app':'http://www.w3.org/2005/app','atom':'http://www.w3.org/2005/Atom'}

for workspace in doc.findall('app:workspace', namespaces):
    for collection in workspace.findall("app:collection", namespaces):
        for title in collection.findall("atom:title", namespaces):
            print(title.text)