2 Replies Latest reply: Sep 14, 2011 12:10 PM by 881648 RSS

    XML De-Serialization question

    881648
      All,

      I am new to java and need some help in deserializing the following APIWebServicesClientConfig.xml file

      <?xml version="1.0" encoding="utf-8"?>
      <APIWebServicesClientConfig>
      <ArtifactDataFileDir>C:\Temp</ArtifactDataFileDir>
      <ArtifactDataFile>xxx.xml</ArtifactDataFile>
      <LogFileDir>C:\Temp</LogFileDir>
      <LogfileName>APIWebServicesClient1.log</LogfileName>
      <ProjectForgeContextName>ProjectForge.mil</ProjectForgeContextName>
      <ProjectForgeServerHost>xxx.mil</ProjectForgeServerHost>
      <TotalNoOfIssuesToBeImported>5</TotalNoOfIssuesToBeImported>
      <ProjectId>proj1077</ProjectId>
      <TrackerIdTobeUsed>xxxx</TrackerIdTobeUsed>
      <UseSSL>true</UseSSL>
      </APIWebServicesClientConfig>

      I have alredy defined a class named "APIWebServicesClientConfig" which is marked as Serializable.
      Can somebody post few lines of sample code that can deserilize the above .xml file.

      Thanks for your help,
      Soma.
        • 1. Re: XML De-Serialization question
          836548
          878645 wrote:
          I have alredy defined a class named "APIWebServicesClientConfig" which is marked as Serializable.
          Can somebody post few lines of sample code that can deserilize the above .xml file.
          I don't get what do you mean here by deserializing( deserialize the xml or read the xml and deserialized the classes mentioned in the xml nodes). If you have already serialized it, then deserialization is reverse engineering. If you want any help, post the serialization code.
          • 2. Re: XML De-Serialization question
            881648
            Thanks for looking into the post. I am having .xml file with some config information that need to read into a Java class at run time. I think in Java world, it is called as un-marshalling. I found out late last night that
            JAXB can be used. Now thinigs are working fine.

            Some of the issues that I ran into are

            a) The property definition in Java is quite tedious. For example in the class "APIWebServicesClientConfig" I have a property

            public String getArtifactdatafiledir()
            {
                 return Artifactdatafiledir;
            }
            public void setArtifactdatafiledir(String Artifactdatafiledir)
            {
                 this.Artifactdatafiledir = Artifactdatafiledir;
            }

            In .Net world, you can just jave

            public string Artifactdatafiledir
            {
            get;
            set;
            }


            Also, one thing that surpised me is, if I have the property name as combination of upper and lower case letters values are not set properly

            public void setArtifactDataFileDir(String ArtifactDataFileDir)
            {
                 this.ArtifactDataFiledir = ArtifactDatafFleDir;
            }

            It has to be exactly like with only one uppercase letter after 'set'!
            ===========================
            public void setArtifactdatafiledir(String Artifactdatafiledir)
            {
                 this.Artifactdatafiledir = Artifactdatafiledir;
            }

            ============================================

            Also, in the .xml file all the nodes are to be in lowercase!. May be I am new to Java and missing few points here.


            Here is the code for un-marshalling (De-Serialization) which is working well now.


            =============================================================================================================
            public static APIWebServicesClientConfig DeSerializeFromFile(String pathSpec)
                 {
                 
                           APIWebServicesClientConfig pDetails = null;
                           
                           try
                           {
                                JAXBContext context = JAXBContext.newInstance(com.collab.examples.APIWebServicesClientConfig.class);
                                Unmarshaller unmarshaller = context.createUnmarshaller();
                                pDetails = (APIWebServicesClientConfig) unmarshaller.unmarshal(new File(pathSpec));
                           }
                           catch (JAXBException ex)
                           {
                                ex.printStackTrace();
                                
                           }
                           catch (Exception ex)
                           {
                                ex.printStackTrace();
                           }
                           
                           
                           return pDetails;
                 }
            =====================================================================================================