如何修复XMLSyntaxError:标记<some xml =“”标记“ =”“>中的数据过早结束

时间:2019-03-17 21:50:16

标签: python python-2.7 xml-parsing lxml

所以我看到了一些答案,但是似乎没有一个可以解决这个问题。 我正在尝试使用lxml.etree.parse方法解析一个简单的文件,但是我不断收到错误消息

  

lxml.etree.XMLSyntaxError:标记 body 第2行中的数据提前结束   第2行,第32列

我在行上得到错误:

tree = etree.parse( infile, parser )

这是格式正确的简单xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Dont forget me this weekend</body>
</note>

这是我的实现,它是解析xml文件的主要功能(parseXmlFile)

from lxml import etree
from path import path
from FixNS import FixNS
....

def parseXmlFile(xmlFilePath):
    nsPrefixMap = dict()
    fixns = FixNS()
    fixns.fixNS( xmlFilePath )
    infile = fixns.getResult()
    fixns.getNSPrefixMap( nsPrefixMap )

    parser = etree.XMLParser( remove_blank_text = True, ns_clean = True, huge_tree = True )
    tree = etree.parse( infile, parser )
    root = tree.getroot()
    return tree, root, nsPrefixMap, fixns

FixNS类以及辅助函数-为完整性起见添加:

from cStringIO import StringIO
import xml.sax.expatreader
from xml.sax import make_parser, SAXNotRecognizedException, SAXNotSupportedException
from xml.sax.handler import property_lexical_handler, feature_namespaces, feature_validation
from xml.sax.saxutils import XMLGenerator, quoteattr
from blzip import ReadBLZip

class FixNS(XMLGenerator):

    def __init__(self):
        XMLGenerator.__init__(self)

    def fixNS(self, infilename):
        XMLGenerator.__init__(self, StringIO())
        self._out = StringIO()
        self._result = StringIO()
        self._inFileName = infilename
        self._nsDeclPos = 0
        self._wasBLZipped = False
        self._inContent = file(self._inFileName, 'rb').read()
        if self._inContent.startswith('BLZIP'):
            self._inContent = ReadBLZip(self._inFileName)
            self._wasBLZipped = True
        self._knownNsPrefixes = set()
        self._collectedNsPrefixes = dict()
        self._isroot = True
        self._in_entity = 0
        self._in_cdata = 0
        self._line = 0
        self._column = 0
        self._parser = make_parser(['xml.sax.expatreader'])
        self._parser.setContentHandler(self)
        self._parser.setProperty(property_lexical_handler, self)
        try:
            self._parser.setFeature(feature_namespaces, 0)
        except (SAXNotRecognizedException, SAXNotSupportedException):
            pass

        try:
            self._parser.setFeature(feature_validation, 0)
        except (SAXNotRecognizedException, SAXNotSupportedException):
            pass

        self._parser.parse(StringIO(self._inContent))

    def getResult(self):
        return StringIO(self._result.getvalue())

使用 python 2.7 lxml-2.3 。对如何解决此解析错误有帮助吗?

0 个答案:

没有答案