Commons Configuration
  1. Commons Configuration
  2. CONFIGURATION-106

[configuration] XMLConfiguration.save() does not keep element hierarchy

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.0
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:

      Operating System: All
      Platform: All

      Description

      I am using commons.configuration.XMLConfiguration to save some configuration
      elements of my Java 1.4.2 application in a file. If I use nested elements in my
      configuration, this structure is not mapped to the saved XML-File.

      As an example, consider the following simple main method:

      public static void main(String[] args) {
      try

      { XMLConfiguration myXMLConfig = new XMLConfiguration(); myXMLConfig.setProperty("A","valueA"); myXMLConfig.setProperty("A.B","valueA.B"); myXMLConfig.setProperty("A.B.C.D","valueA.B.C.D"); myXMLConfig.setProperty("E.F.G.H","valueE.F.G.H"); myXMLConfig.setProperty("I.J.K.L","valueI.J.K.L"); myXMLConfig.setProperty("I.J.K",null); myXMLConfig.setProperty("I.J",null); myXMLConfig.setProperty("I",null); myXMLConfig.save("testConfig.xml"); }

      catch (ConfigurationException e)

      { System.out.println("ERROR: " + e.toString()); }

      }

      What I expect to get by executing this piece of code is the following (modulo
      formatting issues, of course):

      <?xml version="1.0" encoding="UTF-8"?>
      <configuration>
      <A> valueA
      <B> valueB
      <C>
      <D> valueD
      </D>
      </C>
      </B>
      </A>
      <E>
      <F>
      <G>
      <H> valueE.F.G.H
      </H>
      </G>
      </F>
      </E>
      <I> null
      <J> null
      <K> null
      <L> valueI.J.K.L
      </L>
      </K>
      </J>
      </I>
      </configuration>

      The null-values in I.J.K and so on have only been introduced to show that such a
      workarund does not work, either.
      What I actually receive by storing testConfig.xml is the following:

      <?xml version="1.0" encoding="UTF-8"?>
      <configuration>
      <A>valueA<B>valueA.B<D>valueA.B.C.D</D>
      </B>
      </A>
      <H>valueE.F.G.H</H>
      <L>valueI.J.K.L</L>
      <K>null</K>
      <J>null</J>
      <I>null</I>
      </configuration>

      In my opinion, at least two failures can be seen above:
      a) The elements I, J, K, and L are not nested, but all declared on top level of
      the XML-document
      b) Elements without any value are not automatically inserted as, e.g., element
      C for the path A.B.C.D or elements E, F, and G for E.F.G.H

      I also tried to use HierarchicalXMLConfiguration, but corresponding objects can
      not be saved:
      ERROR: org.apache.commons.configuration.ConfigurationException: Can't save
      HierarchicalXMLConfigurations

      I am using the final Version 1.0 (not RC1 nor RC2) JAR-Files provied by
      http://jakarta.apache.org/commons/configuration/downloads.html

      Best regards,

      Christian Kemmer

        Activity

        Hide
        Christian Kemmer added a comment -

        I reported this bug about four weeks ago. Has there been any work on this during
        the last few weeks? I am just interested in whether I can rely on getting this
        bug fixed in the near future.

        Best regdards,

        Christian Kemmer

        Show
        Christian Kemmer added a comment - I reported this bug about four weeks ago. Has there been any work on this during the last few weeks? I am just interested in whether I can rely on getting this bug fixed in the near future. Best regdards, Christian Kemmer
        Hide
        Oliver Heger added a comment -

        An implementation of the save() method was added to
        HierarchicalXMLConfiguration. Maybe you can give it a try.

        I have some plans to combine the two XMLConfiguration classes, taking the best
        of both. This needs a little experimenting though, so I cannot promise anything
        concrete. But I am going to have a look at it on the coming weekend.

        Oliver

        Show
        Oliver Heger added a comment - An implementation of the save() method was added to HierarchicalXMLConfiguration. Maybe you can give it a try. I have some plans to combine the two XMLConfiguration classes, taking the best of both. This needs a little experimenting though, so I cannot promise anything concrete. But I am going to have a look at it on the coming weekend. Oliver
        Hide
        Chris Winterscheid added a comment -

        I tried the HierachicalXMLConfiguration.

        I get the following when I try to save:

        java.lang.UnsupportedOperationException: Can't save
        HierarchicalXMLConfigurations
        at org.apache.commons.configuration.HierarchicalXMLConfiguration.save
        (HierarchicalXMLConfiguration.java:199)
        at
        org.apache.commons.configuration.HierarchicalXMLConfiguration$FileConfiguration
        Delegate.save(HierarchicalXMLConfiguration.java:251)
        at org.apache.commons.configuration.AbstractFileConfiguration.save
        (AbstractFileConfiguration.java:324)
        at org.apache.commons.configuration.AbstractFileConfiguration.save
        (AbstractFileConfiguration.java:292)
        at org.apache.commons.configuration.AbstractFileConfiguration.save
        (AbstractFileConfiguration.java:260)
        at org.apache.commons.configuration.AbstractFileConfiguration.save
        (AbstractFileConfiguration.java:241)
        at org.apache.commons.configuration.HierarchicalXMLConfiguration.save
        (HierarchicalXMLConfiguration.java:184)
        at config.ConfigUpdate.<init>(ConfigUpdate.java:54)
        at config.ConfigUpdate.main(ConfigUpdate.java:65)
        Exception in thread "main"

        Show
        Chris Winterscheid added a comment - I tried the HierachicalXMLConfiguration. I get the following when I try to save: java.lang.UnsupportedOperationException: Can't save HierarchicalXMLConfigurations at org.apache.commons.configuration.HierarchicalXMLConfiguration.save (HierarchicalXMLConfiguration.java:199) at org.apache.commons.configuration.HierarchicalXMLConfiguration$FileConfiguration Delegate.save(HierarchicalXMLConfiguration.java:251) at org.apache.commons.configuration.AbstractFileConfiguration.save (AbstractFileConfiguration.java:324) at org.apache.commons.configuration.AbstractFileConfiguration.save (AbstractFileConfiguration.java:292) at org.apache.commons.configuration.AbstractFileConfiguration.save (AbstractFileConfiguration.java:260) at org.apache.commons.configuration.AbstractFileConfiguration.save (AbstractFileConfiguration.java:241) at org.apache.commons.configuration.HierarchicalXMLConfiguration.save (HierarchicalXMLConfiguration.java:184) at config.ConfigUpdate.<init>(ConfigUpdate.java:54) at config.ConfigUpdate.main(ConfigUpdate.java:65) Exception in thread "main"
        Hide
        Emmanuel Bourg added a comment -

        Chris, did you try with the latest nightly build ?

        Show
        Emmanuel Bourg added a comment - Chris, did you try with the latest nightly build ?
        Hide
        Christian Kemmer added a comment -

        Hi Oliver,
        hi Emmanuel,

        I cannot compile the latest sources (from 2004-12-14) due to the following
        error:

        compile:
        [javac] Compiling 41 source files to E:\TEMP\commons-configuration-src-
        20041214\commons-configuration\target\classes
        [javac] E:\TEMP\commons-configuration-src-20041214\commons-
        configuration\src\java\org\apache\commons\configuration\AbstractConfiguration
        .java:149: cannot resolve symbol
        [javac] symbol : method arrayIterator (java.lang.Object)
        [javac] location: class org.apache.commons.collections.IteratorUtils
        [javac] return fetchInsertIterator(IteratorUtils.arrayIterator
        (token));
        [javac] ^
        [javac] 1 error

        The required libraries, including commons-collections-3.0.jar, have been
        downloaded by ant before.

        So, I am not able to test the new version of HierarchicalXMLConfiguration. Or
        is there a link to a pre-compiled nightly build, that I missed? I am not
        familiar with the developing process of jakarta-commons as I am only trying to
        use the configuration library for my program.

        Best regards,

        Christian

        Show
        Christian Kemmer added a comment - Hi Oliver, hi Emmanuel, I cannot compile the latest sources (from 2004-12-14) due to the following error: compile: [javac] Compiling 41 source files to E:\TEMP\commons-configuration-src- 20041214\commons-configuration\target\classes [javac] E:\TEMP\commons-configuration-src-20041214\commons- configuration\src\java\org\apache\commons\configuration\AbstractConfiguration .java:149: cannot resolve symbol [javac] symbol : method arrayIterator (java.lang.Object) [javac] location: class org.apache.commons.collections.IteratorUtils [javac] return fetchInsertIterator(IteratorUtils.arrayIterator (token)); [javac] ^ [javac] 1 error The required libraries, including commons-collections-3.0.jar, have been downloaded by ant before. So, I am not able to test the new version of HierarchicalXMLConfiguration. Or is there a link to a pre-compiled nightly build, that I missed? I am not familiar with the developing process of jakarta-commons as I am only trying to use the configuration library for my program. Best regards, Christian
        Hide
        Oliver Heger added a comment -

        This is strange.

        We are using maven for building per default. So it may be possible that the ant
        build file is a bit out of date. Is there any chance that you can try maven
        (http://maven.apache.org/) instead?

        I am also not sure why there are no nightly binaries available for
        configuration, but probably for the same reasons.

        HTH
        Oliver

        Show
        Oliver Heger added a comment - This is strange. We are using maven for building per default. So it may be possible that the ant build file is a bit out of date. Is there any chance that you can try maven ( http://maven.apache.org/ ) instead? I am also not sure why there are no nightly binaries available for configuration, but probably for the same reasons. HTH Oliver
        Hide
        Christian Kemmer added a comment -

        Hi Oliver,

        thanks a lot for that remark. The build from
        commons-configuration-src-20041214.zip has completed without any problems now.

        And, in advance, saving HierarchicalXMLConfigurations really seems to work now.

        I have tried the following main method:

        public static void main(String[] args) {
        try

        { HierarchicalXMLConfiguration myHierarchicalXMLConfig = new HierarchicalXMLConfiguration(); myHierarchicalXMLConfig.setProperty("A", "valueA"); myHierarchicalXMLConfig.setProperty("A.B", "valueA.B"); myHierarchicalXMLConfig.setProperty("A.B.C.D", "valueA.B.C.D"); myHierarchicalXMLConfig.setProperty("E.F.G.H", "valueE.F.G.H"); myHierarchicalXMLConfig.setProperty("I.J.K.L", "valueI.J.K.L"); myHierarchicalXMLConfig.setProperty("I.J.K", null); myHierarchicalXMLConfig.setProperty("I.J", null); myHierarchicalXMLConfig.setProperty("I", null); myHierarchicalXMLConfig.setProperty("A.B", "NEWvalueA.B"); myHierarchicalXMLConfig.save("testConfig.xml"); }

        catch (ConfigurationException e)

        { System.out.println("ERROR: " + e.toString()); }

        }

        which gives me

        <?xml version="1.0" encoding="UTF-8"?>
        <configuration>
        <A>valueA<B>NEWvalueA.B<C>
        <D>valueA.B.C.D</D>
        </C>
        </B>
        </A>
        <E>
        <F>
        <G>
        <H>valueE.F.G.H</H>
        </G>
        </F>
        </E>
        <I>
        <J>
        <K>
        <L>valueI.J.K.L</L>
        </K>
        </J>
        </I>
        </configuration>

        This means that
        a) elements are correctly nested now
        b) elements without any value are inserted correctly now

        So, I say a big THANK YOU to you, Oliver, and to all others who helped resolving
        this issue.

        I am changing the status of this issue to FIXED and just wait for the next
        official release to come, including this fix.

        Best regards,

        Christian

        Show
        Christian Kemmer added a comment - Hi Oliver, thanks a lot for that remark. The build from commons-configuration-src-20041214.zip has completed without any problems now. And, in advance, saving HierarchicalXMLConfigurations really seems to work now. I have tried the following main method: public static void main(String[] args) { try { HierarchicalXMLConfiguration myHierarchicalXMLConfig = new HierarchicalXMLConfiguration(); myHierarchicalXMLConfig.setProperty("A", "valueA"); myHierarchicalXMLConfig.setProperty("A.B", "valueA.B"); myHierarchicalXMLConfig.setProperty("A.B.C.D", "valueA.B.C.D"); myHierarchicalXMLConfig.setProperty("E.F.G.H", "valueE.F.G.H"); myHierarchicalXMLConfig.setProperty("I.J.K.L", "valueI.J.K.L"); myHierarchicalXMLConfig.setProperty("I.J.K", null); myHierarchicalXMLConfig.setProperty("I.J", null); myHierarchicalXMLConfig.setProperty("I", null); myHierarchicalXMLConfig.setProperty("A.B", "NEWvalueA.B"); myHierarchicalXMLConfig.save("testConfig.xml"); } catch (ConfigurationException e) { System.out.println("ERROR: " + e.toString()); } } which gives me <?xml version="1.0" encoding="UTF-8"?> <configuration> <A>valueA<B>NEWvalueA.B<C> <D>valueA.B.C.D</D> </C> </B> </A> <E> <F> <G> <H>valueE.F.G.H</H> </G> </F> </E> <I> <J> <K> <L>valueI.J.K.L</L> </K> </J> </I> </configuration> This means that a) elements are correctly nested now b) elements without any value are inserted correctly now So, I say a big THANK YOU to you, Oliver, and to all others who helped resolving this issue. I am changing the status of this issue to FIXED and just wait for the next official release to come, including this fix. Best regards, Christian
        Hide
        Christian Kemmer added a comment -
            • COM-1601 has been marked as a duplicate of this bug. ***
        Show
        Christian Kemmer added a comment - COM-1601 has been marked as a duplicate of this bug. ***

          People

          • Assignee:
            Unassigned
            Reporter:
            Christian Kemmer
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development