You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xmlutils.py 930 B

12345678910111213141516171819202122232425262728
  1. """
  2. Utilities for XML generation/parsing.
  3. """
  4. import re
  5. from xml.sax.saxutils import XMLGenerator
  6. class UnserializableContentError(ValueError):
  7. pass
  8. class SimplerXMLGenerator(XMLGenerator):
  9. def addQuickElement(self, name, contents=None, attrs=None):
  10. "Convenience method for adding an element with no children"
  11. if attrs is None:
  12. attrs = {}
  13. self.startElement(name, attrs)
  14. if contents is not None:
  15. self.characters(contents)
  16. self.endElement(name)
  17. def characters(self, content):
  18. if content and re.search(r'[\x00-\x08\x0B-\x0C\x0E-\x1F]', content):
  19. # Fail loudly when content has control chars (unsupported in XML 1.0)
  20. # See http://www.w3.org/International/questions/qa-controls
  21. raise UnserializableContentError("Control characters are not supported in XML 1.0")
  22. XMLGenerator.characters(self, content)