实体引用和lxml [英] Entity references and lxml

查看:73
本文介绍了实体引用和lxml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

from cStringIO import StringIO
from lxml import etree

xml = StringIO('''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [
<!ENTITY test "This is a test">
]>
<root>
  <sub>&test;</sub>
</root>''')

d1 = etree.parse(xml)
print '%r' % d1.find('/sub').text

parser = etree.XMLParser(resolve_entities=False)
d2 = etree.parse(xml, parser=parser)
print '%r' % d2.find('/sub').text

这是输出:

'This is a test'
None

如何获取lxml给我'&test;',即原始实体引用?

How do I get lxml to give me '&test;', i.e., the raw entity reference?

推荐答案

未解决"实体保留为元素节点sub

The "unresolved" Entity is left as child node of the element node sub

>>> print d2.find('/sub')[0]
&test;
>>> d2.find('/sub').getchildren()
[&test;]

这篇关于实体引用和lxml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆