'XML tag not found
After extracting XML from CDATA I can't find tags in the extracted XML. If I convert to string and then back to an ElementTree I can find the tags I'm looking for (un-comment the lines marked "UNCOMMENT ME"). Looking for a better / more correct way.
import xml.etree.ElementTree as ElementTree
XML = '''<?xml version="1.0" encoding="UTF-8"?>
<Catalog>
<Data><![CDATA[
<Book>
<Author>George Orwell</Author>
<Title>1984</Title>
</Book>
]]></Data>
</Catalog>
'''
def get_cdata_xml(xml_str: str) -> ElementTree:
xml_root = ElementTree.fromstring(xml_str)
cdata_xml = xml_root.find('.//Data')
return cdata_xml
if __name__ == '__main__':
cdata_xml = get_cdata_xml(XML)
#xml_str = cdata_xml.text # UNCOMMENT ME
#cdata_xml = ElementTree.fromstring(xml_str) #UNCOMMENT ME
# type(cdata_xml) = xml.etree.ElementTree.Element
author = cdata_xml.find('.//Author')
print(author.text)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
