我试图解析一个 xml 文件。我的问题与此相同:
使用python ElementTree解析未知元素的xml文件
我尝试了untubu的解决方案。
它工作得很好。但仅适用于具有单个标签的行
例如:
<some_root_name>
<tag_x>bubbles</tag_x>
</some_root_name>
这很好用但是如果它是这样的:
src = '''\
<review type="review"><link>http://www.openlist.com/new-york-ny/mickey-mantles/27612417/?numReviews=178</link>
'''
它失败了..我有很多这样的例子。我不想超出本机库的使用范围,因为在此之后我将在不同的计算机(prod env)上运行代码,并且我将不得不在那里设置库......它变得混乱......
有没有办法,我可以修改原来的解决方案来解决这个问题。谢谢。
上面链接的代码:
import xml.sax as sax
import xml.sax.handler as saxhandler
import pprint
class TagParser(saxhandler.ContentHandler):
# http://docs.python.org/library/xml.sax.handler.html#contenthandler-objects
def __init__(self):
self.tags = {}
def startElement(self, name, attrs):
self.tag = name
def endElement(self, name):
if self.tag:
self.tags[self.tag] = self.data
self.tag = None
self.data = None
def characters(self, content):
self.data = content
parser = TagParser()
src = '''\
<some_root_name>
<tag_x>bubbles</tag_x>
<tag_y>car</tag_y>
<tag...>42</tag...>
</some_root_name>'''
sax.parseString(src, parser)
pprint.pprint(parser.tags)
异常跟踪:
File "extract_xml.py", line 59, in unittest
sax.parseString(src, parser)
File "C:\Python27\lib\xml\sax\__init__.py", line 49, in parseString
parser.parse(inpsrc)
File "C:\Python27\lib\xml\sax\expatreader.py", line 107, in parse
xmlreader.IncrementalParser.parse(self, source)
File "C:\Python27\lib\xml\sax\xmlreader.py", line 125, in parse
self.close()
File "C:\Python27\lib\xml\sax\expatreader.py", line 217, in close
self.feed("", isFinal = 1)
File "C:\Python27\lib\xml\sax\expatreader.py", line 211, in feed
self._err_handler.fatalError(exc)
File "C:\Python27\lib\xml\sax\handler.py", line 38, in fatalError
raise exception
xml.sax._exceptions.SAXParseException: <unknown>:2:4: no element found