in reply to XML::XPath printToFile function?

XML::XPath is not meant for building in-memory XML documents from scratch. You should probably have a look at either XML::Writer or XML::Generator. The POD documentation for either of those modules should give you adequate documentation for using them.