[英]problems with parsing namespace XML. Showing null. in Python
我的XML文件在这里可用。 虽然我能够从该文件中获取根节点及其子节点。 但是,我无法获得所需的那个。 我想获取<ce:section-title>Methods</ce:section-title>
我已经尝试了xml和lxml包。
当我使用以下内容时,
tree = lxml.etree.parse(fname) #fname is xml filename
root= tree.getroot()
print(root[5].findall("ce:section-title",root.nsmap)
它只是给了我null []括号。 当我使用以下命令时,它给出了相同的空括号:
for item in tree.iter('{http://www.elsevier.com/xml/ja/dtd}ce:section-title'):
print(item)
我确实尝试使用此处提供的解决方案来解决,但此代码出现以下错误:
ns = {"ce":"http://www.elsevier.com/xml/common/dtd"}
print(root.findall("ce:title", ns).text)
AttributeError:“ NoneType”对象没有属性“ text”
任何方向都会有所帮助
它应该与findall(.//ce:section-title, root.nsmap)
。
使用.//
前缀,您将在上下文节点下方的所有级别上搜索section-title
后代。 使用findall(ce:section-title, root.nsmap)
,只能找到直接子元素。
例:
from lxml import etree
tree = etree.parse("data.xml") # Your XML
root = tree.getroot()
for e in root.findall(".//ce:section-title", root.nsmap):
print(e.text)
输出:
Abstract
Keywords
Introduction
Materials and methods
Results
The appearing species by taxon
List of regional appearing species
Discussion
Acknowledgments
References
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.