Reaching xml tags with namespaces in python - jupyter

Viewed 25

Hello I try to parse this xml pasted below but my code doesn't return anything. Anyone can help me please?

<data>
<row>
<document>
<doc:document xmlns:ed="http://mail.yahoo.com/d/folders/1" xmlns:reslvd="http://mail.yahoo.com/d/folders/1/234" xmlns:ct="http://mail.yahoo.com/d/folders/cool/storybro" xmlns:doc="http://mail.yahoo.com/d/folders/nice/alldone">
<ed:EI>
<ed:SID>9865-346</ed:SID>
</doc:document>
</document>
</row>
</data>

my code is:

x = []
for value in root.findall(".//*[@name='ed:SID']"):
    x.append(value.text)
1 Answers

Your predicate is wrong: //*[@name='ed:SID'] means: find any element which has an attribute called name and which itself has a value of ed:SID. Your sample xml doesn't have an element which meets these requirements.

Also, your sample xml is not well formed -<ed:EI> should be closed like this: <ed:EI/>.

Also, you have to deal with the namespace.

Finally, if I understand you correctly, this is what you're looking for:

for value in root.findall(".//{*}SID"):
   x.append(value.text)
print(x)

The output, given your sample xml, is

['9865-346']
Related