0

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??

  • Did you check this link ? http://stackoverflow.com/questions/17831304/can-jaxb-get-xml-comments-when-unmarshalling – Hirak May 14 '14 at 06:24
  • That link just explains how to extract the comment. What I am looking for is how to keep the comments and format intact in the file while I add new entries to it. Once I unmarshall the xml and marshall it again, the old format, ordering and comments all are gone. – NotFoundYet May 15 '14 at 05:57
  • When you unmarshall the xml, the comments are gone. Because Jaxb ignores them. That is why you need to follow the cited post and extract the comments from xml and put those in your POJOs. Then when you are marshalling again, you need to put the comments back into the xml – Hirak May 15 '14 at 06:03
  • Ok.And what about the format? Setting JAxb.Output_Format to be true does not seem to work. All elemets are being printed in one line the new file. Any idea how to maintain the same format as the old file? – NotFoundYet May 15 '14 at 06:35
  • Nopes.... I dont remember any preserve formatting functionality in Jaxb. May I know why preserving format is important? If you want for displaying needs, maybe you can yourself "pretty print" the xml... like http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java – Hirak May 15 '14 at 06:39

0 Answers0