我使用lxml来解析和对象化路径中的xml文件,我有很多模型和xsd,每个对象模型都映射到特定的定义类,例如,如果xml以model标记开头,那么它就是一个dataModel,如果它以page tag开头,那么它就是viewModel。在
我的问题是如何有效地检测xml文件从哪个标记开始,然后用适当的xsd文件解析它,然后将其对象化
files = glob(os.path.join('resources/xml', '*.xml'))
for f in files:
xmlinput = open(f)
xmlContent = xmlinput.read()
if xsdPath:
xsdFile = open(xsdPath)
# xsdFile should retrieve according to xml content
schema = etree.XMLSchema(file=xsdFile)
xmlinput.seek(0)
myxml = etree.parse(xmlinput)
try:
schema.assertValid(myxml)
except etree.DocumentInvalid as x:
print "In file %s error %s has occurred." % (xmlPath, x.message)
finally:
xsdFile.close()
xmlinput.close()
我自愿放弃阅读和治疗文件,集中精力解决你的问题:
希望这对某人有用,即使我之前没看过。在
相关问题 更多 >
编程相关推荐