Edit: Rephrased entire question.
I am writing a Java program to analyze some metadata on Salesforce and also modify some files.
I am trying to read a metadata file of type Profile into Profile object Salesforce provides: https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_profile.htm
I was able to do so by the following code: http://paste.ofcode.org/CRZMZsfcwdTmXz4zzCwJqY
When I want to modify the metadata. e.g. add Field Permission etc. and save it into an XML file back again. I am using this code: http://paste.ofcode.org/fZQZaGu3DMjfsHdndwq2WD
However, the output is different than what I downloaded from Salesforce.
Source from Salesforce: http://paste.ofcode.org/tq6S48VyHx7MJFtai5ZqUM
Output from my app: http://paste.ofcode.org/FnRSMfNrsM4CrEQ5e9Nc5Y
Please assist :) Thanks !