微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如果不存在,请使用getElementsByTagName跳过元素

如何解决如果不存在,请使用getElementsByTagName跳过元素

我有一个脚本,用于解析XML文件以查找某些属性。但是,当我尝试定义不存在的属性时,它将引发错误解决此问题的最佳方法是什么?

例如,此代码查找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

输入以下XML文件

<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>

由于未定义标签<author>,因此代码在最后一个元素上触发。如果XML文件中的标记未定义,如何将author变量定义为某个值?

谢谢!

解决方法

您可以使用new Map(orderedObj) 块来解决此问题,您的代码将如下所示:

try except

您可以找到有关try / except块如何工作的更多信息here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。