开发者

Using objectify to get items with a different namespace prefix

<?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!&开发者_高级运维lt;/content>
    <dd:country_code>USA</dd:country_code>
</entry>

I would like to use lxml.objectify to access both 'Hello World!' and 'USA'. How can it be done? I am not concerned with efficiency, just parsimony. I've tried everything I can think of to no avail.


With this setup:

import lxml.objectify as objectify
import io

content='''\
<?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>
</feed>'''

doc=objectify.parse(io.BytesIO(content))
tree=doc.getroot()

The short and quick way:

print(list(tree.entry.iterchildren()))
# ['Hello World!', 'USA']

Or the more specific way:

print(tree.entry["content"])
# Hello World!

to handle namespaces:

print(tree.entry["{http://example.com/ns/1.0}country_code"])
# USA

This method of accessing namespaces is documented here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜