Python 使用objectify获取具有不同命名空间前缀的项 你好,世界! 美国
我想使用lxml.objectify访问“Hello World!”和‘美国’。怎样才能做到呢?我不关心效率,只关心节约。我尝试了我能想到的一切,但都没有成功。使用此设置:Python 使用objectify获取具有不同命名空间前缀的项 你好,世界! 美国,python,xml,lxml,objectify,Python,Xml,Lxml,Objectify,我想使用lxml.objectify访问“Hello World!”和‘美国’。怎样才能做到呢?我不关心效率,只关心节约。我尝试了我能想到的一切,但都没有成功。使用此设置: <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:dd="http://example.com/ns/1.0" xml:lang="en-US"> <entry>
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dd="http://example.com/ns/1.0" xml:lang="en-US">
<entry>
<content type="html">Hello World!</content>
<dd:country_code>USA</dd:country_code>
</entry>
或者更具体的方式:
print(list(tree.entry.iterchildren()))
# ['Hello World!', 'USA']
要处理名称空间,请执行以下操作:
print(tree.entry["content"])
# Hello World!
这种访问名称空间的方法是
print(tree.entry["content"])
# Hello World!
print(tree.entry["{http://example.com/ns/1.0}country_code"])
# USA