无法使用lxml

时间:2019-06-28 18:27:18

标签: python-3.x lxml

我正在尝试解析具有&includes;的xml文件。在里面,但是失败了

文件开头

<!DOCTYPE gdml [
        <!ENTITY materials SYSTEM "materialsOptical.xml"> 
        <!ENTITY solids SYSTEM "solids.xml"> 
        <!ENTITY matrices SYSTEM "matrices.xml">
]>


<gdml xmlns:gdml="http://cern.ch/2001/Schemas/GDML"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schema/gdml.xsd">


<define>
<constant name="PI" value="1.*pi"/>
&matrices;

正在使用的代码是

from lxml import etree
    #root = etree.fromstring(currentString)
    parser = etree.XMLParser(resolve_entities=True)
    root = etree.parse(filename, parser=parser)

但是我得到一个错误

 File "/usr/share/freecad/Mod/GDML/importGDML.py", line 702, in processGDML
    root = etree.parse(filename, parser=parser)
   File "src/lxml/etree.pyx", line 3426, in lxml.etree.parse
   File "src/lxml/parser.pxi", line 1839, in     lxml.etree._parseDocument
   File "src/lxml/parser.pxi", line 1865, in lxml.etree._parseDocumentFromURL
   File "src/lxml/parser.pxi", line 1769, in lxml.etree._parseDocFromFile
   File "src/lxml/parser.pxi", line 1162, in lxml.etree._BaseParser._parseDocFromFile
  File "src/lxml/parser.pxi", line 600, in lxml.etree._ParserContext._handleParseResultDoc
  File "src/lxml/parser.pxi", line 710, in lxml.etree._handleParseResult
  File "src/lxml/parser.pxi", line 639, in  lxml.etree._raiseParseError
<class 'lxml.etree.XMLSyntaxError'>: Failure to process entity matrices, line 14, column 11 (detector.gdml, line 14)

1 个答案:

答案 0 :(得分:0)

好吧,我已经将矩阵文件下载为matrices.gdml,其中父文件将其称为matrices.xml。

现在工作