You are appending messageE to the root element twice, first to rootE and then later to root. both which point to the same document root. This might be where the exception is thrown.