简体   繁体   中英

How to find a node from a file-like xml object in Python3?

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.

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