简体   繁体   中英

How to print CData content while parsing xml local file with python?

So I have a XML file from a local folder that I want to scrape using Python. It has CData and looks like this:

<?xml version="1.0" encoding="utf-8"?>
   <trial xmlns="urn::trial">
      <drksId><![CDATA[DRKS00000024]]></drksId>
      <firstDrksPublishDate><![CDATA[2008-09-05T12:36:54.000+02:00]]></firstDrksPublishDate>
      <firstPartnerPublishDate><![CDATA[2004-01-15T00:00:00.000+01:00]]></firstPartnerPublishDate>
      ......

I tried:

import xml.etree.ElementTree as Et
tree=Et.parse(filename)
root=tree.getroot()
print(root.find('drksId').text)

Output: I am getting root.find('drksId') as None. Thanks in advance

Try to search element considering namespace:

ns = {'ns': 'urn::trial'}
drksId = root.find('./ns:drksId', ns)
print(drksId.text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM