简体   繁体   中英

Trying to check if a tag exists in XML before parsing

I need to check the existence of certain tags in an XML file before parsing it; I'm using Element Tree in Python. Reading here , I tried writing this:


tgz_xml = f"https://www.ncbi.nlm.nih.gov/pmc/utils/oa/oa.fcgi?id=PMC8300416" 
response = urllib.request.urlopen(tgz_xml).read()
tree = ET.fromstring(response)


for OA in tree.findall('OA'):
  records = OA.find('records')
  if records is None:
    print('records missing')
  else:
    print('records found')

I need to check if the "records" tag exists. I don't get an error, but this doesn't print out anything. What did I do wrong? Thank you!

When parsing this XML document variable tree already points to element OA , so when searching for this element expression tree.findall('OA') returns an empty list and loop isn't executed. Remove that line and code will be executed:

import xml.etree.ElementTree as ET 
from urllib.request import urlopen

tgz_xml = f"https://www.ncbi.nlm.nih.gov/pmc/utils/oa/oa.fcgi?id=PMC8300416" 
with urlopen(tgz_xml) as conn:
  response = conn.read()
  tree = ET.fromstring(response)

  records = tree.find('records')
  if records is None:
    print('records missing')
  else:
    print('records found')

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