1

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!

2 Answers 2

1
child = elem.find('LastName')
if child != None : sn = child.text

etc

In the nodes where there is no LastName Element, Find is returning None and None doesn't have a text property is what the error is telling you.

Sign up to request clarification or add additional context in comments.

1 Comment

... child is not None ... would be more pythonic.
0

Here's how the code could look like to resolve the issue you face:

    import xml.etree.ElementTree as etree

    infile = r'test.xml'

    authors = []
    tree = etree.parse(infile)
    root = tree.getroot()
    for elem in tree.iter(tag='Author'):
        snode = elem.find('LastName')
        if snode is not None:
            sn = snode.text
        fnode = elem.find('Initials')
        if fnode is not None:
            fn = fnode.text
        if (fnode is not None) and (snode is not None):
            authors.append(fn + ' ' + sn)
    for x in authors:
        print (x)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.