refactoring to support toXML()