Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用objectify获取具有不同命名空间前缀的项 你好,世界! 美国_Python_Xml_Lxml_Objectify - Fatal编程技术网

Python 使用objectify获取具有不同命名空间前缀的项 你好,世界! 美国

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>

我想使用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>
    <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