I understand the need for cloning the variable to $xmldata - but could be not just initialise the new var as a JRegistry rather than JObject?

I think that would work and still target all your cases successfully