This is my first attempt at both Python and pulling info from XML files, so apologies for the newbie nature of the question.
I'm tying to extract author names from an XML file where the info is structured like so:
<Author ValidYN="Y">
<LastName>Duck</LastName>
<ForeName>Donald</ForeName>
<Initials>D</Initials>
</Author>
Every so often there is an entry that looks like this:
<Author ValidYN="Y">
<CollectiveName>Some Corp</CollectiveName>
</Author>
The code I have works fine with the first example, but falls over if it comes across the second, and displays an AttributeError: 'NoneType' object has no attribute 'text' message. To my very basic understanding of what's happening I think the error is arising simply because there is nothing for it to find. What I can't work out is how to have it ignore the second example and continue looking for the next author.
Here's the code:
import xml.etree.ElementTree as etree
infile = r'C:\temp\test.xml'
authors = []
tree = etree.parse(infile)
root = tree.getroot()
for elem in tree.iter(tag='Author'):
sn = elem.find('LastName').text
fn = elem.find('Initials').text
authors.append(fn + ' ' + sn)
for x in authors:
print (x)
Any help gratefully received!