0

I just started learning Python and have to write a program that parses xml files. I have multiple entries as seen below and I need, as a starting point, to return all the different d:Name entries in a list.

Unfortunately, I can't manage to use findall with prefixes.

import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
lst =  tree.findall('.//{d}Name')

I read that if d is a prefix, I need to use the URI instead of a. But I don't understand which is the URI in my case, or how to make a successful search when i have the following file.

I have an XML that looks like this (simplified):

feed xml:base="http://projectserver/ps/_api/">
 <entry>
  <id>
     http://projectserver/ps/_api/ProjectServer/EnterpriseResources('some id...')
  </id>
  <content type="application/xml">
    <m:properties>
      <d:Name>
        WHAT I NEED
      </d:Name>
    </m:properties>
  </content>
 </entry>
 <entry>
   ...

This bypassed my problem so thank you!

If you are using Python 3.8 or later, this post may help: link – Jim Rhodes

So I ran the following which returned the list of tags, where i found the {URI}Name which I then used to do the search properly.

for elem in tree.iter():
    print(elem.tag)
LaPatata
  • 1
  • 2
  • @Jim Rhodes is right, --> d is a **namespace prefix**, search with that term additionally here in the questions and you will find solutions, I needed them lateley. – BitLauncher Mar 20 '22 at 20:26
  • 2
    If you are using Python 3.8 or later, this post may help: [link](https://stackoverflow.com/questions/62110439/how-to-use-python-xml-findall-to-find-vimagedata-rid-rid7-otitle-1-ren/62117710#62117710) – Jim Rhodes Mar 20 '22 at 20:26
  • Thank you for the quick reply. Unfortunately not. When I make the request to the server API it doesn't return them. – LaPatata Mar 20 '22 at 20:26

0 Answers0