How to detect starting tag of xml and then parse and objectify

conpilergarm

New Member
I'm using lxml to parse and objectify xml files in a path, I have a lot of model and xsd's, each object model maps to certain defined classes, for example if xml starts with model tag so it is a dataModel and if it starts with page tag it is a viewModel.My question is how to detect in efficient way that xml file starts with which tag and then parse it with an appropriate xsd file and then objectify it\[code\]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()\[/code\]
 
Back
Top