AttributeError:' NodeList'对象没有属性' getElementsByTagName'

时间:2016-03-14 21:11:50

标签: python xml dom

我正在尝试从xml文件中提取文本内容。 xml文件是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<brca:tcga_bcr xsi:schemaLocation="http://tcga.nci/bcr/xml/clinical/brca/2.7 http://tcga-data.nci.nih.gov/docs/xsd/BCR/tcga.nci/bcr/xml/clinical/brca/2.7/TCGA_BCR.BRCA_Clinical.xsd" schemaVersion="2.7" xmlns:brca="http://tcga.nci/bcr/xml/clinical/brca/2.7" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:admin="http://tcga.nci/bcr/xml/administration/2.7" xmlns:clin_shared="http://tcga.nci/bcr/xml/clinical/shared/2.7" xmlns:shared="http://tcga.nci/bcr/xml/shared/2.7" xmlns:brca_shared="http://tcga.nci/bcr/xml/clinical/brca/shared/2.7" xmlns:shared_stage="http://tcga.nci/bcr/xml/clinical/shared/stage/2.7" xmlns:brca_nte="http://tcga.nci/bcr/xml/clinical/brca/shared/new_tumor_event/2.7/1.0" xmlns:nte="http://tcga.nci/bcr/xml/clinical/shared/new_tumor_event/2.7" xmlns:rx="http://tcga.nci/bcr/xml/clinical/pharmaceutical/2.7" xmlns:rad="http://tcga.nci/bcr/xml/clinical/radiation/2.7">
    <admin:admin>
        <admin:bcr xsd_ver="1.17">Nationwide Children's Hospital</admin:bcr>
        <admin:file_uuid xsd_ver="2.6">6CEF6ECD-264E-4DF6-8419-9E4C564DA7B2</admin:file_uuid>
        <admin:batch_number xsd_ver="1.17">85.84.0</admin:batch_number>
        <admin:project_code xsd_ver="">TCGA</admin:project_code>
        <admin:disease_code xsd_ver="2.6">BRCA</admin:disease_code>
        <admin:day_of_dcc_upload xsd_ver="1.17">21</admin:day_of_dcc_upload>
        <admin:month_of_dcc_upload xsd_ver="1.17">1</admin:month_of_dcc_upload>
        <admin:year_of_dcc_upload xsd_ver="1.17">2016</admin:year_of_dcc_upload>
        <admin:patient_withdrawal>
            <admin:withdrawn>false</admin:withdrawn>
        </admin:patient_withdrawal>
    </admin:admin>

我收到以下错误:

  

AttributeError:&#39; NodeList&#39;对象没有属性&#39; getElementsByTagName&#39;

我正在使用python 2.7。这是我的代码的一部分。我无法弄清楚出了什么问题。有什么建议吗?

from xml.dom import minidom
xmldoc = minidom.parse('A0SD.xml')
bcr = xmldoc.getElementsByTagNameNS('*','tcga_bcr')
patient_info = bcr.getElementsByTagName('admin')

1 个答案:

答案 0 :(得分:0)

getElementsByTagNameNS返回具有指定标记的所有节点的列表。因此bcrNodeList。您不能仅getElemnetsByTagName NodeList Nodebcr。您需要迭代admin以获取每个节点中的tcga_bcr标记。或者,如果您希望只有一个bcr = xmldoc.getElementsByTagNameNS('*','tcga_bcr')[0] 标记,那么您可以只获取列表的第一个元素:

(let [date-format java.time.format.DateTimeFormatter/ISO_LOCAL_DATE]
  (.. (java.time.LocalDate/parse "2015-02-13" date-format)
      (plusDays 90)
      (format date-format)))