Skip element with getElementsByTagName if it doesn't exist

200 views Asked by At

I have a script that parses an XML file looking for certain attributes. However, when I try to define an attribute that doesnt exist, it throws an error. What is the best way to resolve this?

For example, this code looks for all works given by an API.

    elif respCode == '4':
        works = xdoc.getElementsByTagName('works')[0]
        print "     " +  'Works found: ' + str(len(works.getElementsByTagName('work'))) + ' different works'
        for work in works.getElementsByTagName('work'):
            author = work.attributes["author"].value
            title = work.attributes["title"].value
            editionCount = work.attributes["editions"].value
            date = work.attributes["lyr"].value
            format = work.attributes["format"].value
            owi = work.attributes["owi"].value
            wi = work.attributes["wi"].value

When fed the following XML file

<classify xmlns="http://classify.oclc.org">
<response code="4"/>
<!-- Classify is a product of OCLC Online Computer Library Center: http://classify.oclc.org -->
<workCount>7</workCount>
<start>0</start>
<maxRecs>25</maxRecs>
<orderBy>thold desc</orderBy>
<input type="isbn">1</input>
<works>
<work author="Barlow, Alfred E. (Alfred Ernest), 1861-1914 | Geological Survey of Canada" editions="33" format="eBook" holdings="270" hyr="2018" itemtype="itemtype-book-digital" lyr="1904" owi="12532881" schemes="DDC LCC" title="Reprint of a report on the origin, geological relations and composition of the nickel and copper deposits of the Sudbury Mining District, Ontario, Canada" wi="9090518"/>
<work author="Skillen, James W." editions="2" format="Book" holdings="237" hyr="2014" itemtype="itemtype-book" lyr="2014" owi="1361997817" schemes="DDC LCC" title="The good of politics : a biblical, historical, and contemporary introduction" wi="849787504"/>
<work author="Buchori, Binny | Buchori, Binny [Contributor] | Husain, Thamrin, 1974- | Salampessy, Zairin, 1968-" editions="4" format="Book" holdings="21" hyr="2011" itemtype="itemtype-book" lyr="2001" owi="475047565" schemes="DDC LCC" title="Ketika semerbak cengkih tergusur asap mesiu : tragedi kemanusiaan Maluku di balik konspirasi militer, kapitalis birokrat, dan kepentingan elit politik" wi="48642781"/>
<work author="Bauman, Amy" editions="3" format="Book" holdings="11" hyr="2009" itemtype="itemtype-book" lyr="2009" owi="481071496" schemes="DDC" title="Pirate's treasure : a peek-a-boo adventure" wi="615048025"/>
<work author="Stanton, Geoffrey | CfBT Education Trust" editions="3" format="eBook" holdings="9" hyr="2015" itemtype="itemtype-book-digital" lyr="2008" owi="4889708365" schemes="DDC" title="Learning matters : making the 14-19 reforms work for learners : by emphasising learning programmes as well as qualifications : by learning from previous initiatives" wi="751807280"/>
<work author="Ide, Arthur Frederick" editions="2" format="Book" holdings="5" hyr="1985" itemtype="itemtype-book" lyr="1985" owi="64427876" schemes="DDC LCC" title="Idol worshippers in America : Phyllis Schlafly, Ronald Reagan, Jerry Falwell, and the Moral Majority on women, work, and homosexuality : with a parallel translation and critical commentary on Genesis 19" wi="79169264"/>
<work editions="3" format="Book" holdings="5" hyr="2020" itemtype="itemtype-book" lyr="2020" owi="10209736909" schemes="DDC" title="52 weeks of socks" wi="1142963815"/>
</works>
</classify>

The code trips on the last element because the tag <author> is not defined. How can I define my author variable to a certain value if the tag in the XML file is undefined?

Thanks!

1

There are 1 answers

1
charbel On BEST ANSWER

You can get around this problem by using try except blocks, your code will look something like this:

try:
    author = work.attributes["author"].value
except:
    author = 'defaultValue'

you can find more information on how try/except block work here