These is the content of a file-like object toc
:
<?xml version='1.0' encoding='utf-8'?>
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1" xml:lang="eng">
<head>
...
</head>
<docTitle>
<text>THE_TEXT_I_WANT</text>
</docTitle>
...
</ncx>
My Python3 codes now:
import xml.etree.ElementTree as ET
# I get toc using open method in zipfile module
# toc : <zipfile.ZipExtFile name='toc.ncx' mode='r' compress_type=deflate>
toc_tree = ET.parse(toc)
for node in toc_tree.iter():
print(node)
print(toc_tree.find('docTitle'))
The for loop can print out all nodes but find
method returns None
. findall
method returns nothing either. Please anybody tell me why? Is there any better solution?
Because there is a (default) namespace in your XML, searching for elements called docTitle
will find nothing, as it is searching for un-namespaced elements called docTitle
. Instead, you need to use clark notation with the full namespace URI:
toc_tree.find('{http://www.daisy.org/z3986/2005/ncx/}docTitle')
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.