2013-02-06 54 views
5

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=Trueresolve_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=Trueresolve_entities=True,所以我的文件失敗。

是否有xml.etree - 解決這些實體的途徑?

回答

1

我的訣竅是使用外部程序xmllint

proc = subprocess.Popen(['xmllint','--noent',fname],stdout=subprocess.PIPE) 
output = proc.communicate()[0] 
tree = ElementTree.parse(StringIO.StringIO(output)) 
相關問題