Honor toJSON/toXML() for XML support

/to @ritch @bajtos 

The PR allows customization of JSON to XML conversion using toJSON()/toXML(). It also adds some test cases for XML media types.
How is this handled for the JSON case? Can we extract common parts into a shared method?