【问题标题】:How to access the unresolved entities in lxml and Python?如何访问 lxml 和 Python 中未解析的实体?
【发布时间】:2017-06-19 22:33:38
【问题描述】:

我有一个格式良好的 XML,代码如下:

    <fxp-connector name="streaming" host="&fxvmnsrv.host;" port="&fxvmnsrv.port;" mode="INITIATOR">
        <message type="1" .../>
       ....
    </fxp-connector>
    .....

我正在解析它:

import lxml.etree as ET
parser = ET.XMLParser(resolve_entities=False)
tree = ET.parse(inCfgFileName, parser)
....
outFile = open(outCfgFileName, "w")
......
e = tree.findall('fxp-connector[@name="streaming"]')
.....
tree.write(outFile)

在输出中该行变为:

&fxvmnsrv.host;&fxvmnsrv.port;<fxp-connector name="streaming" host="" port="" mode="INITIATOR">

...

print e[0] 打印&lt;message .../&gt; 标签 如何访问这些实体引用?我尝试了一些东西,但到目前为止没有运气。

【问题讨论】:

  • 你尝试了什么,outFile是什么,请显示一些代码
  • Dmitry,我用代码示例更新了问题

标签: python xml lxml


【解决方案1】:

找到他们。它们被插入到根中,即与前面的&lt;fxp-connector&gt; 处于同一级别。

for i in range(20):
print i, root[i]

产生:

9 &fxvmnsrv.host;
10 &fxvmnsrv.port;
11 <Element fxp-connector at 0xffffffff7b7386c8> #this is the <fxp-connector tag>

【讨论】:

    猜你喜欢
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 2013-10-01
    相关资源
    最近更新 更多