We wrote a similar utility for our ORM-for-SaaS library some time ago, using lxml if available and falling back to ElementTree:
https://github.com/ducksboard/libsaas/blob/master/libsaas/xm...
Bear in mind that both approaches are lossy - trying to support every XML quirk would quickly lead to reimplementing the libxml wheel...