xml.etree我一直在使用lxml.etree
來解析XML腳本:如何解決外部實體與像lxml.etree
from lxml import etree
parser = etree.XMLParser(load_dtd=True, resolve_entities=True)
tree = etree.parse('main.xml', parser=parser)
我需要load_dtd=True
和resolve_entities=True
是已經&emptyEntry;
從globals.xml
解決:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE map SYSTEM "globals.xml" [
<!ENTITY dirData "${DATADIR}">
]>
<map
xmlns:map="http://my.dummy.org/map"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsschemaLocation="http://my.dummy.org/map main.xsd"
>
&emptyEntry; <!-- from globals.xml -->
<entry><key>KEY</key><value>VALUE</value></entry>
<entry><key>KEY</key><value>VALUE</value></entry>
</map>
與globals.xml
<?xml version="1.0" encoding="UTF-8"?>
<!ENTITY emptyEntry "<entry></entry>">
現在我想從非標準lxml
轉移到標準xml.etree
。但是由於xml.etree
不支持load_dtd=True
和resolve_entities=True
,所以我的文件失敗。
是否有xml.etree
- 解決這些實體的途徑?