I am using Jaxb to edit an existing xml file. I have the xsd schema for this file, so i have generated the classes using xjc. To add new entries/elements to the xml file, I convert the file into an object using jaxb. Next, I make changes to this objects ie., add new objects to it ( by instantiating the classes) etc. Then this object is marshalled back to the same xml file. The problem is that the new xml file has different formatting as compared to that of the old one. The Comments are also lost and order of elements/attributes is also changed. Here is an example:
Old.xml:
<?xml version="1.0"?>
<!--
Copyright (c) 2009. All rights reserved.
NAME
old.xml
DESCRIPTION
The components for yii.
NOTES
This component only defines the yii deletion.
-->
<yii-definition
xsi:noNamespaceSchemaLocation="file:../schemas/component-definition.xsd"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
category = "application"
name = "ola"
procedure = ""
type = "ola"
singleton = "false"
home = "j2ee"
base = "application-family">
<association
name = "ola Compensation"
id = "app"
type = "ola-p"
enable = "@{action.install.hcm.comp}"
required = "false">
</association>
<association
name = "ola Benefits"
id = "app"
type = "ola-ben"
enable = "@{action.install.hcm.ben}"
required = "false">
</association>
</yii-definition>
New.xml: (added a new association element)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<component-definition base="application-family" category="application" home="j2ee" name="HCM" procedure="" singleton="false" type="hcm">
<association enable="@{action.install.hcm.comp}" id="app" name="Ola Compensation" required="false" type="ola-comp"/>
<association enable="@{action.install.hcm.ben}" id="app" name="Ola Benefits" required="false" type="ola-ben"/>
<association enable="@{action.install.hcm.supply}" id="app" name="Ola SUPPLY" required="false"/>
<yii-defition>
So, the comments are missing. Some additional attributes like standalone etc are added. And ordering of attributes in the elements is also different. What should be done the maintain the same format , order etc??