xml.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # functions for general XML files
  2. import file_ops
  3. from lxml import etree
  4. # function to check if a XML file is well formatted
  5. def check_xml_well_format(xml_path):
  6. # check params
  7. if (file_ops.is_file(xml_path) == False):
  8. return False
  9. # load the xml file and check for syntax errors
  10. # generic parser
  11. generic_parser = etree.XMLParser(ns_clean = True, remove_comments = True)
  12. xml = None
  13. try:
  14. xml = etree.parse(xml_path, generic_parser)
  15. # some errors are not saved on parser's error_log for some reason
  16. except etree.XMLSyntaxError as e:
  17. print("\nSYNTAX ERRORS ON XML:")
  18. print(e)
  19. return False
  20. # print the parser error_log errors
  21. except:
  22. print("\nOTHER ERRORS ON XML:")
  23. for error in generic_parser.error_log:
  24. print("Line %s || %s" % (error.line, error.message))
  25. return False
  26. # all good!
  27. return True
  28. # function to check if an schema (XSD) file is well formatted
  29. def check_sch_well_format(xsd_path):
  30. # check params
  31. if (check_xml_well_format(xsd_path) == False):
  32. return False
  33. # load the XMLSchema.xsd as a parser for the schema files
  34. schema_tree = etree.parse("XMLSchema.xsd")
  35. schema_schema = etree.XMLSchema(schema_tree)
  36. schema_parser = etree.XMLParser(schema = schema_schema)
  37. # load the custom schema file and check if it has errors
  38. try:
  39. custom_schema_tree = etree.parse(xsd_path, schema_parser)
  40. custom_schema_schema = etree.XMLSchema(custom_schema_tree)
  41. # some errors are not saved on parser's error_log for some reason
  42. except etree.XMLSyntaxError as e:
  43. print("\nSYNTAX ERRORS ON XSD:")
  44. print(e)
  45. return False
  46. except etree.XMLSchemaParseError as e:
  47. print("\nSCHEMA PARSING ERRORS ON XSD:")
  48. print(e)
  49. return False
  50. # print the parser error_log errors
  51. except:
  52. print("\nOTHER ERRORS ON XSD:")
  53. for error in schema_parser.error_log:
  54. print("Line %s || %s" % (error.line, error.message))
  55. return False
  56. # all good!
  57. return True
  58. # function to check an XML against an schema (XSD)
  59. def check_with_sch(xml_path, xsd_path):
  60. # check params
  61. if ((check_xml_well_format(xml_path) == False) or (check_sch_well_format(xsd_path) == False)):
  62. return False
  63. # load the collada file and check for errors agaisnt the custom schema
  64. schema_tree = etree.parse(xsd_path)
  65. schema_schema = etree.XMLSchema(schema_tree)
  66. schema_parser = etree.XMLParser(schema = schema_schema)
  67. try:
  68. xml = etree.parse(xml_path, schema_parser)
  69. # some errors are not saved on parser's error_log for some reason
  70. except etree.XMLSyntaxError as e:
  71. print("\nSYNTAX ERRORS ON CUSTOM SCHEMA CHECKING XML:")
  72. print(e)
  73. return False
  74. # print the parser error_log errors
  75. except etree.XMLSchemaValidateError:
  76. print("\nOTHER ERRORS ON CUSTOM SCHEMA CHECKING XML:")
  77. for error in custom_schema_parser.error_log:
  78. print("Line %s || %s" % (error.line, error.message))
  79. return False
  80. # done!
  81. return True