我正在阅读包含各种<xml>..</xml>
元素的大文件。由于每个XML解析器都有问题,我想为每个<xml>..</xml>
块生成有效的新文件对象。
我开始在Python中继承文件对象,但却陷入了困境。我想,我要拦截以</xml>
开头的每一行并返回一个新的文件对象;也许可以使用yield
。
有人可以指导我朝正确的方向迈出一步吗?
这是我目前的代码片段:
#!/bin/bash/env python
from lxml import etree
from StringIO import StringIO
class handler(file):
def __init__(self, name, mode):
file.__init__(self, name, mode)
def next(self):
return file.next(self)
def listXmls(self):
output = StringIO()
line = self.next()
while line is not None:
output.write(line.strip())
if line.strip() == '</xml>':
yield output
output = StringIO()
try:
line = self.next()
except StopIteration:
break
output.close()
f = handler('myxml.xml', 'r')
for elem in f.listXmls():
print 'm' + elem.getvalue() + 'm'
context = etree.iterparse(elem, events=('end',), tag='id')
for event, element in context:
print element.tag
谢谢!
解决方案(仍然对更好的版本感兴趣):
#!/bin/bash/env python
from lxml import etree
from StringIO import StringIO
class handler(file):
def __init__(self, name, mode):
file.__init__(self, name, mode)
def next(self):
return file.next(self)
def listXmls(self):
output = StringIO()
output.write(self.next())
line = self.next()
while line is not None:
if line.startswith('<?xml'):
output.seek(0)
yield output
output = StringIO()
output.write(line)
try:
line = self.next()
except StopIteration:
break
output.seek(0)
yield output
f = handler('myxml.xml', 'r')
for elem in f.listXmls():
context = etree.iterparse(elem, events=('end',), tag='id')
for event, element in context:
print element.tag
答案 0 :(得分:1)
虽然不能直接回答您的问题,但这可能会解决您的问题:只需在开头添加另一个<xml>
而在结尾添加另一个</xml>
可能会使您的XML解析器接受该文档:
from lxml import etree
document = "<xml>a</xml> <xml>b</xml>"
document = "<xml>" + document + "</xml>"
for subdocument in etree.XML(document):
# whatever